Skip to content

Instantly share code, notes, and snippets.

@yckart
Forked from 140bytes/LICENSE.txt
Last active Dec 22, 2015
Embed
What would you like to do?
randomWord

Thanks to @atk for the "recursive regex"-solution. It is even less than 140 bytes and "inlines" the subset of characters into the function. I think this could be much more interesting than my approach.

function a(b,c,d){d=Math.random()*99;return++c?'aeioubcdfghjklmnpqrstvwxyz'.charAt(c&1?d%19+5:d%5):Array(b+1).join('.').replace(/./g,a)}
function (
a, // an integer, how long should or word be
b, // an array containing the vowels
c, // an array containing the consonants
d, // word-placeholder
e // random()-placeholder
) {
d = '';
for (a = a>>1 || 1; a--;)
d += c[(e=Math.random()) * c.length | 0] + b[e * b.length | 0];
return d
}
function(a,b,c,d,e){d='';for(a=a>>1||1;a--;)d+=c[(e=Math.random())* c.length |0]+b[e * b.length |0];return d}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2013 Yannick Albert <http://yckart.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "randomWord",
"description": 'Generate a "valid" random word with vowels and consonants.',
"keywords": [
"word",
"random",
"generate",
"string",
"lorem"
]
}
<!DOCTYPE html>
<title>randomWord</title>
<div>Expected value: <b>A randomized word with 10 characters.</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var randomWord = function(a,b,c,d,e){d='';for(a=a>>1||1;a--;)d+=c[(e=Math.random())* c.length |0]+b[e * b.length |0];return d};
var vowels = 'aeiou'.split('');
var consonants = 'bcdfghjklmnpqrstvwxyz'.split('');
document.getElementById('ret').innerHTML += randomWord(10, vowels, consonants);
</script>
@atk

This comment has been minimized.

Copy link

@atk atk commented Sep 9, 2013

With a little recursive regex magick, we can incorporate vowels and consonants into the function:

function a(b,c,d){d=Math.random()*99;return++c?'aeioubcdfghjklmnpqrstvwxyz'.charAt(c&1?d%19+5:d%5):Array(b+1).join('.').replace(/./g,a)}
@yckart

This comment has been minimized.

Copy link
Owner Author

@yckart yckart commented Sep 9, 2013

@atk Awesome! I like your idea, really... Problem is, that not any language has the same subset of foreign characters. So I think it's the better choice to let the user define their own character-set/s. Anyway, your Idea is great, I'll note it in the readme...

@atk

This comment has been minimized.

Copy link

@atk atk commented Sep 9, 2013

In that case, you can at least replace a/2|0 with a>>1

@yckart

This comment has been minimized.

Copy link
Owner Author

@yckart yckart commented Oct 13, 2013

@atk Good point, updated it!

@subzey

This comment has been minimized.

Copy link

@subzey subzey commented Nov 14, 2013

@atk, (Array(b+1)+'').replace(/./g,a) would be shorter than Array(b+1).join('.').replace(/./g,a)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment