| layout | language | permalink | command | related_commands | ||
|---|---|---|---|---|---|---|
api-command |
Python |
api/python/random/ |
random |
|
{% apibody %} r.random() → number r.random(number[, number], float=True) → number r.random(integer[, integer]) → integer {% endapibody %}
Generate a random number between given (or implied) bounds. random takes zero, one or two arguments.
- With zero arguments, the result will be a floating-point number in the range
[0,1)(from 0 up to but not including 1). - With one argument x, the result will be in the range
[0,x), and will be integer unlessfloat=Trueis given as an option. Specifying a floating point number without thefloatoption will raise an error. - With two arguments x and y, the result will be in the range
[x,y), and will be integer unlessfloat=Trueis given as an option. If x and y are equal an error will occur, unless the floating-point option has been specified, in which case x will be returned. Specifying a floating point number without thefloatoption will raise an error.
Note: The last argument given will always be the 'open' side of the range, but when generating a floating-point number, the 'open' side may be less than the 'closed' side.
Example: Generate a random number in the range [0,1)
r.random().run(conn)Example: Generate a random integer in the range [0,100)
r.random(100).run(conn)
r.random(0, 100).run(conn)Example: Generate a random number in the range (-2.24,1.59]
r.random(1.59, -2.24, float=True).run(conn)