Name | random() |
||||
---|---|---|---|---|---|
Examples |
for i in range(100): r = random(50) stroke(r*5) line(50, i, 50+r, i) for i in range(100): r = random(-50, 50) print(r) # Get a random element from an array words = "apple", "bear", "cat", "dog" index = int(random(len(words))) # Same as int(random(4)) print(words[index]) # Prints one of the four words | ||||
Description |
Generates random numbers. Each time the random() function is called, it returns an unexpected value within the specified range. 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. |
||||
Syntax | random(high) random(low, high) | ||||
Parameters |
| ||||
Related |
randomSeed() noise() |
Updated on Tue Feb 27 14:07:12 2024.
If you see any errors or have comments, please let us know.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License