Generates random numbers.


example picture for random()
1def setup():
2    py5.random_seed(42)
3    for i in range(100):
4        s = py5.random()
5        py5.stroke(255*s)
6        r = py5.random(80)
7        py5.line(20, i, 20+r, i)
1def setup():
2    for i in range(100):
3        r = py5.random(-50, 50)
4        py5.println(r)


Generates random numbers. Each time the random() function is called, it returns an unexpected value within the specified range. This function’s randomness can be influenced by random_seed().

If no parameters are passed to the function, it will return a float between zero and one.

If only one parameter is passed to the function, it will return a float between zero and the value of the high parameter. For example, random(5) returns values between 0 and 5 (starting at zero, and up to, but not including, 5).

If two parameters are specified, the function will return a float with a value between the two values. For example, random(-5, 10.2) returns values starting at -5 and up to (but not including) 10.2. To convert a floating-point random number to an integer, use the int() function, or alternatively, consider using random_int().

This function makes calls to numpy to generate the random values.


random() -> float
random(high: float, /) -> float
random(low: float, high: float, /) -> float


  • high: float - upper limit

  • low: float - lower limit

Updated on November 08, 2021 12:26:18pm UTC