Crickets Chirping

mon qui si, mon qui d’où

Random Sampling in JavaScript

with one comment

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.

Written by banksean

December 25th, 2007 at 4:14 pm

Posted in Code,General

One Response to 'Random Sampling in JavaScript'

Subscribe to comments with RSS

  1. 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

Leave a Reply