Document the "random" module.

This commit is contained in:
Bob Nystrom
2015-11-09 07:37:06 -08:00
parent 82706b74fc
commit 902289d7dd
16 changed files with 128 additions and 35 deletions
+7 -2
View File
@@ -1,5 +1,10 @@
^title Random Module
^title Module "random"
**TODO**
This module provides a simple, fast pseudo-random number generator.
It is an optional module. You can omit it from your application by setting the
preprocessor constant `WREN_OPT_RANDOM` to `0`.
It contains a single class:
* [Random](random.html)
+90 -2
View File
@@ -1,7 +1,95 @@
^title Random Class
**TODO**
A simple, fast pseudo-random number generator. Internally, it uses the [well
equidistributed long-period linear PRNG][well] (WELL512a).
[well]: https://en.wikipedia.org/wiki/Well_equidistributed_long-period_linear
Each instance of the class generates a sequence of randomly distributed numbers
based on the internal state of the object. The state is initialized from a
*seed*. Two instances with the same seed generate the exact same sequence of
numbers.
It must be imported from the [random][] module:
:::wren
import "random" for Random
[random]: ../
## Constructors
### Random.**new**()
Creates a new generator whose state is seeded based on the current time.
:::wren
var random = Random.new()
### Random.**new**(seed)
Creates a new generator initialized with [seed]. The seed can either be a
number, or a non-empty sequence of numbers. If the sequnce has more than 16
elements, only the first 16 are used. If it has fewer, the elements are cycled
to generate 16 seed values.
:::wren
Random.new(12345)
Random.new("appleseed".codePoints)
## Methods
**TODO**
### **float**()
Returns a floating point value between 0.0 and 1.0, including 0.0, but excluding
1.0.
:::wren
var random = Random.new(12345)
System.print(random.float()) //> 0.53178795980617
System.print(random.float()) //> 0.20180515043262
System.print(random.float()) //> 0.43371948658705
### **float**(end)
Returns a floating point value between 0.0 and `end`, including 0.0 but
excluding `end`.
:::wren
var random = Random.new(12345)
System.print(random.float(0)) //> 0
System.print(random.float(100)) //> 20.180515043262
System.print(random.float(-100)) //> -43.371948658705
### **float**(start, end)
Returns a floating point value between `start` and `end`, including `start` but
excluding `end`.
:::wren
var random = Random.new(12345)
System.print(random.float(3, 4)) //> 3.5317879598062
System.print(random.float(-10, 10)) //> -5.9638969913476
System.print(random.float(-4, 2)) //> -1.3976830804777
### **int**(end)
Returns an integer between 0 and `end`, including 0 but excluding `end`.
:::wren
var random = Random.new(12345)
System.print(random.int(1)) //> 0
System.print(random.int(10)) //> 2
System.print(random.int(-50)) //> -22
### **int**(start, end)
Returns an integer between `start` and `end`, including `start` but excluding
`end`.
:::wren
var random = Random.new(12345)
System.print(random.int(3, 4)) //> 3
System.print(random.int(-10, 10)) //> -6
System.print(random.int(-4, 2)) //> -2
+2 -2
View File
@@ -22,7 +22,7 @@
<nav class="big">
<ul>
<li><a href="../">Modules</a></li>
<li><a href="./">Random</a></li>
<li><a href="./">random</a></li>
</ul>
<section>
<h2>random classes</h2>
@@ -35,7 +35,7 @@
<table>
<tr>
<td><a href="../">Modules</a></td>
<td><a href="./">Random</a></td>
<td><a href="./">random</a></td>
</tr>
<tr>
<td colspan="2"><h2>random classes</h2></td>