MCPcopy Index your code
hub / github.com/processing/p5.js / random

Function random

src/math/random.js:7–380  ·  view source on GitHub ↗

* @module Math * @submodule Random * @for p5

(p5, fn)

Source from the content-addressed store, hash-verified

5 */
6
7function random(p5, fn){
8 // variables used for random number generators
9 const randomStateProp = '_lcg_random_state';
10 // Set to values from http://en.wikipedia.org/wiki/Numerical_Recipes
11 // m is basically chosen to be large (as it is the max period)
12 // and for its relationships to a and c
13 const m = 4294967296;
14 // a - 1 should be divisible by m's prime factors
15 const a = 1664525;
16 // c and m should be co-prime
17 const c = 1013904223;
18 let y2 = 0;
19
20 // Linear Congruential Generator that stores its state at instance[stateProperty]
21 fn._lcg = function(stateProperty) {
22 // define the recurrence relationship
23 this[stateProperty] = (a * this[stateProperty] + c) % m;
24 // return a float in [0, 1)
25 // we've just used % m, so / m is always < 1
26 return this[stateProperty] / m;
27 };
28
29 fn._lcgSetSeed = function(stateProperty, val) {
30 // pick a random seed if val is undefined or null
31 // the >>> 0 casts the seed to an unsigned 32-bit integer
32 this[stateProperty] = (val == null ? Math.random() * m : val) >>> 0;
33 };
34
35 /**
36 * Sets the seed value for the <a href="#/p5/random">random()</a> and
37 * <a href="#/p5/randomGaussian">randomGaussian()</a> functions.
38 *
39 * By default, <a href="#/p5/random">random()</a> and
40 * <a href="#/p5/randomGaussian">randomGaussian()</a> produce different
41 * results each time a sketch is run. Calling `randomSeed()` with a constant
42 * argument, such as `randomSeed(99)`, makes these functions produce the same
43 * results each time a sketch is run.
44 *
45 * @method randomSeed
46 * @param {Number} seed seed value.
47 *
48 * @example
49 * function setup() {
50 * createCanvas(100, 100);
51 *
52 * background(200);
53 *
54 * // Get random coordinates.
55 * let x = random(0, 100);
56 * let y = random(0, 100);
57 *
58 * // Draw the white circle.
59 * circle(x, y, 10);
60 *
61 * // Set a random seed for consistency.
62 * randomSeed(99);
63 *
64 * // Get random coordinates.

Callers 15

random.jsFile · 0.70
setupFunction · 0.50
regenerateFunction · 0.50
setupFunction · 0.50
constructorMethod · 0.50
greetFunction · 0.50
drawFunction · 0.50
drawFunction · 0.50
jiggleZoogFunction · 0.50
jiggleMethod · 0.50
jiggleMethod · 0.50
setupFunction · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected