Random

Random float

Randomness has many uses in science, art, statistics, cryptography, gaming, gambling, and other fields.

All the functionality you need is contained in the random package, a sub-package of numpy.

  • seed(): sets the random seed, so that your results are reproducible between simulations. As an argument, it takes an integer of your choosing. If you call the function, no output will be generated.
  • rand(): if you don’t specify any arguments, it generates a random float between zero and one.
# Import numpy as np
import numpy as np

# Set the seed
np.random.seed(123)

# Generate and print random float
print(np.random.rand())

randint()

randint() is a function of the random package to generate integers randomly. It will generate random integers from num1 to num2 - 1 (not including num 2).

import numpy as np
np.random.randint(num1, num2)

Example: Generates the integer 4, 5 or 6 randomly. 7 is not included.

# Import numpy and set seed
import numpy as np
np.random.seed(123)

# Use randint() to simulate a dice
print(np.random.randint(4,7))