Skip to content

Instantly share code, notes, and snippets.

@unascribed
Last active September 13, 2015 04:58
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 unascribed/500f6acaed7d01657abd to your computer and use it in GitHub Desktop.
Save unascribed/500f6acaed7d01657abd to your computer and use it in GitHub Desktop.
o='';r=new Random();args[0].chars.any{o+=it;if(it.letter)(r.nextGaussian()*5+10).times{o+=(char)(768+r.nextInt(48))}}print o
// By making this a Groovy script, the weight of
// a full Java-like class definition can be avoided
////
// Originally, this part of the answer used the 'def'
// keyword to infer the type of the variable, but it
// turns out you can define a variable in Groovy
// without any type at all.
buffer = '';
rand = new Random();
// There used to be a for loop here using def, but now it's a
// .any closure. .any saves a byte over .each.
////
// Groovy automatically defines an args variable for
// scripts, which we use here.
args[0].chars.any {
// As in Java, += works on a String. Previous versions of the
// answer used a StringBuffer, but a String works just as well.
buffer += it;
// Another Groovy extension; we can use the primitive char like
// a Character object, and use .letter instead of .isLetter()
if (it.letter)
// Since Gaussian is already a normal distribution with mean 0
// stddev 1, we can fix that with a quick multiply and add.
((rand.nextGaussian() * 5) + 10).times {
// I use the literal number '768' here, as it's the same size
// as a Java char literal, and Groovy doesn't have char literals.
buffer += (char)(768+rand.nextInt(48));
}
}
// Finally, we can take a quick shortcut here by using the
// parentheses-less syntax for print in Groovy. Using print
// instead of println saves a whole two bytes!
print buffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment