Numpy Random | Random Module
Python NumPy random module
The NumPy random is a module help to generate random numbers.
Import NumPy random module
1 2 |
|
np.random.random()
This function generates float value between 0.0 to 1.0 and returns ndarray if you will give shape.
1 2 3 4 |
|
1 2 3 4 5 6 |
|
Click here to jump on Python NumPy tutorial
np.random.randint()
The random integer function generates single random integer number from given range and if the shape will give then return ndarray.
1 2 3 4 5 6 7 |
|
np.random.seed()
The random module generates random number but next time you want to generate the same number then seed() will help.
1 2 3 |
|
1 2 3 4 5 6 7 8 9 10 |
|
Nex time generate the same 3D array using the same seed value (10).
1 2 3 |
|
1 2 3 4 5 6 7 8 9 10 |
|
Note: The seed function accepts value up to 2^32 -1 (4294967295).
np.random.rand()
The rand() function work like random() but it accept shape and return ndarray which contain random values between 0.0 to 1.0.
1 2 |
|
1 2 3 4 |
|
np.random.randn()
The randn() function work like rand() function but it reurn samples of standerd normalise distribution value.
1 2 |
|
1 2 3 4 |
|
np.random.choice()
If you have sequence values and want to get random single value then the random choice() function is the best choice.
1 2 3 |
|
1 2 |
|
Let’s try to get the number of choice from sequence x using for loop.
1 2 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
np.random.permutation()
If you want to generate some permutation of sequence then use random permutation() function.
1 2 |
|
1 2 |
|
Learn more about random sampling then click here
Multiply sea night grass fourth day sea lesser rule open subdue female fill which them Blessed, give fill lesser bearing multiply sea night grass fourth day sea lesser
Emilly Blunt
December 4, 2017 at 3:12 pm