Difference between revisions of "API:RNG::next"

From Spherical
Jump to: navigation, search
(New page: RNG::next())
 
(Improving headings some more)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
{{DISPLAYTITLE:RNG::next()}}
 
{{DISPLAYTITLE:RNG::next()}}
  
<tt>'''RNG::next()'''</tt> generates a pseudorandom floating-point number between 0.0 and 1.0 (exclusive).
+
The <tt>'''RNG::next()'''</tt> method generates a random floating-point number in the range <tt>[0,1)</tt>.
  
==== Usage ====
+
=== Usage ===
 
  ''number'' = ''rng_object''.'''next'''();
 
  ''number'' = ''rng_object''.'''next'''();
  
 
__TOC__
 
__TOC__
  
== Example ==
+
== API Information ==
  
  /*
+
=== Description ===
  * Shuffle a list of numbers (Fisher-Yates)
+
 
  */
+
<tt>'''RNG::next()'''</tt> generates a pseudorandom floating-point number greater than or equal to 0 but less than 1 and returns the value generated. The number generated is entirely determined by the current state of the <tt>RNG</tt> instance before the call (see <tt>[[API:RNG::state|RNG::state]]</tt>). After calling this function, the state is advanced so a different value will be generated next time.  Therefore if you save the RNG's state before calling <tt>RNG.next()</tt>, then restore it afterwards and call <tt>RNG.next()</tt> again, the same value will be returned.
 +
 
 +
=== Parameters ===
 +
 
 +
This method has no parameters.
 +
 
 +
=== Return Value ===
 +
 
 +
A floating-point number in the range <tt>[0,1)</tt>.
 +
 
 +
== Examples ==
 +
 
 +
=== Shuffle an array ===
 +
 
 +
let items = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
 
   
 
   
let numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
 
 
  let rnGen = new RNG();
 
  let rnGen = new RNG();
  for (let i = numbers.length - 1; i > 0; --i) {
+
  for (let i = items.length - 1; i > 0; --i) {
     let j = Math.floor(rnGen.next() * (i + 1));  // integer, [0,i]
+
     let j = Math.floor(rnGen.next() * (i + 1));  // discrete [0,i]
     let orig_i = numbers[i];
+
     let orig_i = items[i];
     numbers[i] = numbers[j];
+
     items[i] = items[j];
     numbers[j] = orig_i;
+
     items[j] = orig_i;
 
  }
 
  }
 +
 +
== See Also ==
 +
 +
<tt>
 +
* [[API:RNG::state|RNG::state]]
 +
</tt>
  
 
[[Category:Sphere 2 API]]
 
[[Category:Sphere 2 API]]

Latest revision as of 03:53, 10 August 2017


The RNG::next() method generates a random floating-point number in the range [0,1).

Usage

number = rng_object.next();

API Information

Description

RNG::next() generates a pseudorandom floating-point number greater than or equal to 0 but less than 1 and returns the value generated. The number generated is entirely determined by the current state of the RNG instance before the call (see RNG::state). After calling this function, the state is advanced so a different value will be generated next time. Therefore if you save the RNG's state before calling RNG.next(), then restore it afterwards and call RNG.next() again, the same value will be returned.

Parameters

This method has no parameters.

Return Value

A floating-point number in the range [0,1).

Examples

Shuffle an array

let items = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

let rnGen = new RNG();
for (let i = items.length - 1; i > 0; --i) {
    let j = Math.floor(rnGen.next() * (i + 1));  // discrete [0,i]
    let orig_i = items[i];
    items[i] = items[j];
    items[j] = orig_i;
}

See Also