Skip to content

Instantly share code, notes, and snippets.

@arnorhs
Last active May 21, 2020 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnorhs/c0e171fce4d202163f8d81f265ea1036 to your computer and use it in GitHub Desktop.
Save arnorhs/c0e171fce4d202163f8d81f265ea1036 to your computer and use it in GitHub Desktop.
/**
* from https://stackoverflow.com/a/49434653
*/
export const boxMullerTransformedRand = (): number => {
let u = 0, v = 0
while (u === 0) u = Math.random() //Converting [0,1) to (0,1)
while (v === 0) v = Math.random()
let num = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v)
num = num / 10.0 + 0.5 // Translate to 0 -> 1
if (num > 1 || num < 0) return boxMullerTransformedRand() // resample between 0 and 1
return num
}
const chars = `englishisawestgermaniclanguagethatwasfirstspokeninearlymedievalenglandandeventuallybecameagloballinguafrancaitisnamedaftertheanglesoneofthegermanictribesthatmigratedtotheareaofgreatbritainthatlatertooktheirnameenglandbothnamesderivefromangliaapeninsulaonthebalticseaenglishismostcloselyrelatedtofrisianandlowsaxonwhileitsvocabularyhasbeensignificantlyinfluencedbyothergermaniclanguagesparticularlynorseanorthgermaniclanguageaswellaslatinandfrenchenglishhasdevelopedoverthecourseofmorethanyearstheearliestformsofenglishagroupofwestgermanicingvaeonicdialectsbroughttogreatbritainbyanglosaxonsettlersinthethcenturyarecollectivelycalledoldenglishmiddleenglishbeganinthelatethcenturywiththenormanconquestofenglandthiswasaperiodinwhichenglishwasinfluencedbyoldfrenchinparticularthoughitsoldnormandialectearlymodernenglishbeganinthelatethcenturywiththeintroductionoftheprintingpresstolondontheprintingofthekingjamesbibleandthestartofthegreatvowelshift`
const symbols = '....??:!'
export const randomChar = (collection = chars) => (
collection[Math.floor(Math.random() * collection.length)]
)
export const randomWord = () => {
const len: number = Math.ceil(boxMullerTransformedRand() * 10) + 1
const arr = new Array(len)
for (let i = 0; i < len; i++) {
arr[i] = randomChar()
}
return arr.join('')
}
export const randomSentence = () => {
const len = Math.floor(Math.random() * 15) + 3
const arr = new Array(len)
for (let i = 0; i < len; i++) {
arr[i] = randomWord()
}
const sentance = arr.join(' ')
return sentance[0].toUpperCase() + sentance.substring(1)
}
export const randomParagraph = () => {
const len = Math.floor(Math.random() * 8) + 2
const arr = new Array(len)
for (let i = 0; i < len; i++) {
arr[i] = randomSentence()
}
return arr.map(str => str + randomChar(symbols)).join(' ')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment