Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>if you don't need the seeding capability, just use <code>Math.random()</code> and build helper functions around it (eg. <code>randRange(start, end)</code>).</p> <p>I'm not sure what RNG you're using, but it's best to know and document it so you're aware of it's characteristics and limitations.</p> <p>Like Starkii said, Mersenne Twister is a good PRNG, but it isn't easy to implement. If you want to do it yourself try implementing a <a href="http://en.wikipedia.org/wiki/Linear_congruential_generator" rel="nofollow noreferrer">LCG</a> - it's very easy, has decent randomness qualities (not as good as Mersenne Twister), and you can use some of the popular constants.</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>function RNG(seed) { // LCG using GCC's constants this.m = 0x80000000; // 2**31; this.a = 1103515245; this.c = 12345; this.state = seed ? seed : Math.floor(Math.random() * (this.m - 1)); } RNG.prototype.nextInt = function() { this.state = (this.a * this.state + this.c) % this.m; return this.state; } RNG.prototype.nextFloat = function() { // returns in range [0,1] return this.nextInt() / (this.m - 1); } RNG.prototype.nextRange = function(start, end) { // returns in range [start, end): including start, excluding end // can't modulu nextInt because of weak randomness in lower bits var rangeSize = end - start; var randomUnder1 = this.nextInt() / this.m; return start + Math.floor(randomUnder1 * rangeSize); } RNG.prototype.choice = function(array) { return array[this.nextRange(0, array.length)]; } var rng = new RNG(20); for (var i = 0; i &lt; 10; i++) console.log(rng.nextRange(10, 50)); var digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; for (var i = 0; i &lt; 10; i++) console.log(rng.choice(digits));</code></pre> </div> </div> </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload