Random
The random class provides a range of methods for producing random values.
The random object is instansiated for free:
const random = new Random()
// examples
random.int()
random.point()
Meta Data
| title | Random |
| dependencies | () |
| unused_keys | () |
| unknown_keys | () |
| filepath_exists | True |
| path | random |
| filepath | random.js |
| clean_files | () |
-
ClassDeclarationclass
Random
extends None-
dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])
Generate an integer between 0 and the given min. Given 1 (default), the result will be either 0 or 1
random.int(10) // 7 random.int(50) // 30 random.int(300) // 220 -
dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])
Select and return a random item from an array.
Use this method to randomly choose one element from a collection of items.
const colors = ['red', 'blue', 'green', 'yellow'] random.choice(colors) // 'green' const points = [new Point(10, 20), new Point(30, 40), new Point(50, 60)] random.choice(points) // Point { x: 30, y: 40 } -
dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])
Return a random index for the given array.
Use this to get a valid random position within an array's bounds.
const items = ['apple', 'banana', 'cherry', 'date'] random.index(items) // 2 items[random.index(items)] // 'banana' -
dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])
Generate a new Point with random x and y coordinates.
Use this to create a random Point instance with coordinates scaled by the multiplier. Values ≤2 use floats, larger values use integers by default.
random.point(100) // Point { x: 47, y: 83 } random.point(1) // Point { x: 0.234, y: 0.891 } random.point(500, 'float') // Point { x: 234.56, y: 412.89 } -
dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])
Randomly shuffle all points be a random amount between 0 and a multilper of the radius. For example if the point is 20px, a
maxof.5would plot the center of the point anywhere within the full point area. Providing a value of1(default) would plot within a 40px width and height (circular) area. (Math.PI * 2)This is because the
maxvalue computes a percenile of the radius of the origin position, resolving anywhere within the diameter area if the point. -
dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])
return two random values between 0 and 1 multiplied by the given value
multiplier. This is similar to the random.point() function, except it only returns an object of {x,y} -
dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])
Generate random 2D coordinates using polar distribution.
Use this to create randomly distributed points following a normal distribution in both x and y dimensions, useful for particle effects and natural scatter patterns.
random.polar2D() // [0.8234, -1.2156] const [x, y] = random.polar2D() const point = new Point(centerX + x * radius, centerY + y * radius)
ExpressionStatement:
dict_keys(['type', 'expression', 'pos'])VariableDeclarationPerform random() on the X and Y.
- If a point is given, randomize to the max of the given point
- If a single number is given, assume square
- If no params are given, discover the stage size.
Use
Point.random()for the same form, as a new pointlet p = new Point() p.randomize(100, 400) p.randomize(100) // 100, 100 p.randomize(new Point(100, 400)) // 100, 400if one of the keys is undefined, no change occurs:
p.setXY(500,700) p.randomize(100, undefined) // 100, 700 p.randomize(undefined, 400) // 500, 400 p.randomize(undefined, undefined) // 500, 700 // no change occurs.Note; no params will randomize as much as possible (the stage size)
p.randomize() // 800, 600Other features would be nice:
-
random in rect (like stage):
p.randomize(rect|dimensions)
-
Randomize other values, e.g radius, colors
p.randomize(['x', 'y', 'radius', 'mass'])
// randomize x to max 400, radius to max 50 p.randomize({ x: 400, radius: 50})
-
In the future, the origin; randomize relative to a point:
p.randomize({point, relative: true})
The minimum value the point(value) can be, before the automatic 'float' method is used over the 'int' method.