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

View File

@ -1,4 +1,4 @@
^title Core Module
^title Module "core"
Because Wren is designed for [embedding in applications][embedding], its core
module is minimal and is focused on working with objects within Wren. For

View File

@ -22,7 +22,7 @@
<nav class="big">
<ul>
<li><a href="../">Modules</a></li>
<li><a href="./">Core</a></li>
<li><a href="./">core</a></li>
</ul>
<section>
<h2>core classes</h2>
@ -47,7 +47,7 @@
<table>
<tr>
<td><a href="../">Modules</a></td>
<td><a href="./">Core</a></td>
<td><a href="./">core</a></td>
</tr>
<tr>
<td colspan="2"><h2>core classes</h2></td>

View File

@ -16,7 +16,7 @@ built directly into the language itself: [numbers][], [strings][], etc.
The core module is always available and can't be removed.
* [Core](core)
* [core](core)
## Optional
@ -31,8 +31,8 @@ possible.
There are a couple of optional modules:
* [Meta](meta)
* [Random](random)
* [meta](meta)
* [random](random)
## CLI
@ -43,6 +43,6 @@ applications that want to embed Wren.
[libuv]: http://libuv.org
* [IO](io)
* [Scheduler](scheduler)
* [Timer](timer)
* [io](io)
* [scheduler](scheduler)
* [timer](timer)

View File

@ -1,4 +1,4 @@
^title IO Module
^title Module "io"
**TODO**

View File

@ -22,7 +22,7 @@
<nav class="big">
<ul>
<li><a href="../">Modules</a></li>
<li><a href="./">IO</a></li>
<li><a href="./">io</a></li>
</ul>
<section>
<h2>io classes</h2>
@ -36,7 +36,7 @@
<table>
<tr>
<td><a href="../">Modules</a></td>
<td><a href="./">IO</a></td>
<td><a href="./">io</a></td>
</tr>
<tr>
<td colspan="2"><h2>io classes</h2></td>

View File

@ -1,4 +1,4 @@
^title Meta Module
^title Module "meta"
**TODO**

View File

@ -22,7 +22,7 @@
<nav class="big">
<ul>
<li><a href="../">Modules</a></li>
<li><a href="./">Meta</a></li>
<li><a href="./">meta</a></li>
</ul>
<section>
<h2>meta classes</h2>
@ -35,7 +35,7 @@
<table>
<tr>
<td><a href="../">Modules</a></td>
<td><a href="./">Meta</a></td>
<td><a href="./">meta</a></td>
</tr>
<tr>
<td colspan="2"><h2>meta classes</h2></td>

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)

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

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>

View File

@ -1,4 +1,4 @@
^title Scheduler Module
^title Module "scheduler"
**TODO**

View File

@ -22,7 +22,7 @@
<nav class="big">
<ul>
<li><a href="../">Modules</a></li>
<li><a href="./">Scheduler</a></li>
<li><a href="./">scheduler</a></li>
</ul>
<section>
<h2>scheduler classes</h2>
@ -35,7 +35,7 @@
<table>
<tr>
<td><a href="../">Modules</a></td>
<td><a href="./">Scheduler</a></td>
<td><a href="./">scheduler</a></td>
</tr>
<tr>
<td colspan="2"><h2>scheduler classes</h2></td>

View File

@ -1,4 +1,4 @@
^title Timer Module
^title Module "timer"
**TODO**

View File

@ -22,7 +22,7 @@
<nav class="big">
<ul>
<li><a href="../">Modules</a></li>
<li><a href="./">Timer</a></li>
<li><a href="./">timer</a></li>
</ul>
<section>
<h2>timer classes</h2>
@ -35,7 +35,7 @@
<table>
<tr>
<td><a href="../">Modules</a></td>
<td><a href="./">Timer</a></td>
<td><a href="./">timer</a></td>
</tr>
<tr>
<td colspan="2"><h2>timer classes</h2></td>

View File

@ -40,10 +40,10 @@ foreign class Random {
foreign seed_(n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15, n16)
foreign float()
float(max) { float() * max }
float(min, max) { float() * (max - min) + min }
float(end) { float() * end }
float(start, end) { float() * (end - start) + start }
foreign int()
int(max) { (float() * max).floor }
int(min, max) { (float() * (max - min)).floor + min }
int(end) { (float() * end).floor }
int(start, end) { (float() * (end - start)).floor + start }
}

View File

@ -42,10 +42,10 @@ static const char* randomModuleSource =
" foreign seed_(n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15, n16)\n"
"\n"
" foreign float()\n"
" float(max) { float() * max }\n"
" float(min, max) { float() * (max - min) + min }\n"
" float(end) { float() * end }\n"
" float(start, end) { float() * (end - start) + start }\n"
"\n"
" foreign int()\n"
" int(max) { (float() * max).floor }\n"
" int(min, max) { (float() * (max - min)).floor + min }\n"
" int(end) { (float() * end).floor }\n"
" int(start, end) { (float() * (end - start)).floor + start }\n"
"}\n";