Skip to content

Instantly share code, notes, and snippets.

@rileyjshaw
Created August 18, 2019 21:36
Show Gist options
  • Save rileyjshaw/32729031967c093718805bfd67a5fdb6 to your computer and use it in GitHub Desktop.
Save rileyjshaw/32729031967c093718805bfd67a5fdb6 to your computer and use it in GitHub Desktop.
print a list of 50 random words (weighted lengths) with scrabble letter frequency
const normalize = a => a.reduce((arr, val, i) => {arr[i + 1] = arr[i] + val; return arr;}, [0]).map((v, i, arr) => v / arr[arr.length - 1]);
const getWeighted = (a, r) => a.findIndex(v => v > r);
const letters = 'abcdefghijklmnopqrstuvwxyz'.split('');
const letterWeights = normalize([9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1]);
const wordLengthWeights = normalize([0, 1, 7, 4, 4, 4, 5, 3, 4, 2, 1, 1, 1, 1, 1, 1, 1]); // 1, 2, 3...
console.log(Array.from({length: 50}, (_, i) =>
Array.from({length: getWeighted(wordLengthWeights, Math.random())}, () => letters[getWeighted(letterWeights, Math.random())]).join('')
).sort((a, b) => a.length - b.length).join('\n'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment