Random Sampling in JavaScript
I’m working on a personal project that requires me to take random samples from arrays in JavaScript. I’ll write about the bigger project in detail later, but for now here’s a piece that may be of use all by itself.
Context:
If you were to randomly increment integers in a JavaScript Array using plain ol’ Math.random() you might end up with something like this (blue=10000, green=1000, red=100 samples, respectively):
But what if you want to sample from a Normal distribution?
Then you might want to use some code I wrote.
Enjoy, send me bug reports, do whatever.
var a = new Array(10);
//add some stuff to a
var i = a.sampleIndex();
var randomSample = a[i];
alfred ronald
11 Aug 08 at 8:47 pm