02 Apr Random module in Numpy
To work with Random numbers, Numpy has a module called random. To use the module, import it at the beginning of a Python program as shown below:
1 2 3 |
from numpy import random |
Let us see some examples:
- Generate a random number
- Generate a random array with a fixed size
- Generate one of the random values based on an array of values
Generate a Random number
To generate a random number, we have used the randint() method of the random module. Let us see an example:
1 2 3 4 5 6 7 8 |
from numpy import random # Get random number from 0 to 5 n = random.randint(5) print("Random number= ",n) |
Output
1 2 3 |
Random number= 4 |
Generate a random array with a fixed size
We will generate random elements of an array using the random.randint() method. The size of the array is set using the size parameter. Let us see an example:
1 2 3 4 5 6 7 8 |
from numpy import random # get 3 random numbers as array elements from 0 to 10 n = random.randint(10, size=(3)) print("Random number= ",n) |
Output
1 2 3 |
Random number= [6 0 1] |
Generate one of the random values based on an array of values
One of the random values based on an array of values can be generated using the choice() method of the random module. Let us see an example:
1 2 3 4 5 6 7 8 |
from numpy import random # get a random number from the following array values n = random.choice([10, 20, 30, 40, 50, 60, 70, 80, 90, 100]) print("Random number= ",n) |
Output
1 2 3 |
Random number= 100 |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments