Created
August 30, 2013 14:31
-
-
Save mef51/6390469 to your computer and use it in GitHub Desktop.
generates pronouncable words
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
randomInt = (min, max) -> | |
Math.floor(Math.random() * (max - min + 1) + min) | |
capitalize = (string) -> | |
string[0].toUpperCase() + string.slice(1).toLowerCase() | |
getRandomSyllable = -> | |
vowels = 'aeiouy' | |
consonants = 'bcdfghjklmnpqrstvwxz' | |
consonantSounds = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z','th','ch'] | |
# consonants.charAt(randomInt(0, consonants.length - 1)) + vowels.charAt(randomInt(0, vowels.length - 1)) | |
consonantSounds[randomInt(0, consonantSounds.length - 1)] + vowels.charAt(randomInt(0, vowels.length - 1)) | |
getRandomName = (length) -> | |
name = '' | |
name += getRandomSyllable() for i in [1..length] | |
name | |
# Generates a random array of length `length` with values from `fn(i)` where `i` is the position in the array | |
generateRandomArray = (length, fn) -> | |
result = [] | |
for i in [1..length] | |
result.push fn(i) | |
result | |
# generates random arrays of length `length` | |
randomIntArray = (length, min, max) -> | |
randomInt min, max for i in [1..length] | |
randomNameArray = (length, minNameLength, maxNameLength) -> | |
getRandomName randomInt(minNameLength, maxNameLength) for i in [1..length] | |
# counts occurrences of `elem` in `array` | |
countOccurrences = (elem, array) -> | |
count = 0 | |
count++ for e in array when e is elem | |
count | |
randomMapNamestoInts = (length, nameOpts, intOpts) -> | |
names = randomNameArray length, nameOpts.min, nameOpts.max | |
ints = randomIntArray length, intOpts.min, intOpts.max | |
result = {} | |
for name, i in names | |
result[name] = ints[i] | |
result | |
console.log randomMapNamestoInts 100, {min:2, max:4}, {min: 0, max: 128} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment