Last active
May 21, 2020 23:34
-
-
Save arnorhs/c0e171fce4d202163f8d81f265ea1036 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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