-
Notifications
You must be signed in to change notification settings - Fork 6
The rand library
The math/rand library makes use of the functions found in Golang's math/rand library. For this reason, like its Golang equivalent, it should not be used for cryptographic applications.
The API is quite different from the Go version, because getting a random number from the RNG is necessarily a command.
We define the following types:
Exponential = struct()
Normal = struct()
Random = struct(elements)
Seed = struct()
Shuffle = struct(elements)
Zipf = struct(s, v float, imax int)
Then the following commands are available:
This seeds the random number generator for when you need it to be deterministic. If you don't, it's given a strong random seed on initialization.
This chooses a random value from the range given by the elements field of the Random value, according to the type of elements:
- If
elementsis a list, it selects a random element of the list. E.g:get x from Random ["one", "two", "three]. - If it's an integer
i, it selects a random integer in0::i. - If it's
int, it selects a random integer from the whole 64-bit rang of integers. - If it's a float
x, it selects a random float between 0 andx. - If it's
float, it selects a random float between 0 and 1. - If it's
bool, it selects randomly fromtrueandfalse. - If it's the name of an enum type, it selects a random element of the enum.
- If it's a pair of integers, it selects a random integer from the range.
- If it's a pair of floats, it selects a random float from the range.
This returns a random permutation of the range given by the elements field of the Shuffle value, according to the type of elements:
- If
elementsis a list, it returns a permutation of the list. - If it's the name of an enum type, it returns a permutation of the elements of the enum.
- If it's a pair of integers, if returns a permutation of the integers in that range, e.g.
get x from Shuffle 0::10will get a permutation of the numbers0...9.
This gets a random float greater than or equal to 0, with an exponential distribution and mean both equal to 1.
This returns a random float with a normal distribution having mean 0 and standard deviation 1.
This gets values k ∈ [0, z.imax] such that P(k) is proportional to (z.v+k)^(-z.s). Note that in this one case, the top of the range (z.imax) is among the possibilities.
🧿 Pipefish is distributed under the MIT license. Please steal my code and ideas.
- Getting started
- Language basics
- The type system and built-in functions
- Functional Pipefish
- Encapsulation
- Imperative Pipefish
-
Imports and libraries
- The files library
- The fmt library
- The html library
- The math library
- The math/big library
- The math/cmplx library
- The math/rand library
- The path library
- The path/filepath library
- The reflect library
- The regexp library
- The sql library
- The strings library
- The terminal library
- The time library
- The unicode library
- Advanced Pipefish
- Developing in Pipefish
- Deployment
- Appendices