API:RNG::next

From Spherical
Revision as of 14:55, 8 August 2017 by Bruce Pascoe (talk | contribs) (New page: RNG::next())
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


RNG::next() generates a pseudorandom floating-point number between 0.0 and 1.0 (exclusive).

Usage

number = rng_object.next();

Contents

Example

/* 
 *  Shuffle a list of numbers (Fisher-Yates)
 */

let numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
let rnGen = new RNG();
for (let i = numbers.length - 1; i > 0; --i) {
    let j = Math.floor(rnGen.next() * (i + 1));  // integer, [0,i]
    let orig_i = numbers[i];
    numbers[i] = numbers[j];
    numbers[j] = orig_i;
}