Updated July 7, 2023
Introduction to NumPy random
A Random number is a number that is generated without following any logic or pattern so that it cannot be used again for any process; such kind of random number generation is very useful in the security or encryption of crucial information, and so serve this purpose python has a library that performs a variety of numerical operation very easily called Numpy (Numerical Python) which can be used in the generation of Pseudo-Random numbers using BitGenerator that gives different combinations of sequences and the Generator then transforms the sequences into numbers following probability distribution.
Syntax
Numpy can generate a Random sequence for various data types like float, int, array, etc. Random number generation is performed by importing the Random package from the Numpy library. The basic syntax for numpy random generation is
from numpy import random
randint(int)
Examples of NumPy random
Let us discuss a different example of using different random generation techniques for better understanding.
Example #1
Code:
from numpy import random
a = random.randint(10)
b= random.randint(50)
print(a)
print(b)
Output:
In this example, we have imported the random package from the numpy library to operate. We have generated random integer a & b using the numpy random.randint() syntax and the value we gave in the bracket is the maximum limit for generating a random number. The output 4 is below the 10 we declared, and 44 is below the 50 we have declared.
Example #2
We Numpy random to generate a list of float values in this example,
Code:
from numpy import random
a = random.rand()
b= random.rand(5)
print(a)
print(b)
Output:
In this example, we have used the random.rand() syntax, which allows us to generate float values. We haven’t declared any number in the variable’ a’, so it generates a random float from 0 to 1 by default. In variable ‘b,’ we have declared a limit of 5, so we have a list of 5 random float values. We can declare any number to generate float values of that limit we have declared.
Example #3
In this example, we’ll discuss the random generation of numpy arrays using a similar syntax we used in the previous examples.
Code:
from numpy import random
a = random.randint(10, size=(3))
b= random.randint(50, size=(7))
print(a)
print(b)
Output:
We have used the randint syntax and declared the maximum limit and the sequence size we are generating in this example. This method is a random generation of 1-dimensional arrays. For ‘a’, we declared the maximum limit to be 10 and the sequence length to be 3; for ‘b,’ we declared the maximum limit to be 50 and the sequence length to be 7, and we got the corresponding output.
Similarly, we’ll try to generate 2-dimensional arrays in the following code.
Code:
from numpy import random
a = random.randint(10, size=(3,3))
b= random.randint(50, size=(5,5))
print(a)
print(b)
Output:
Using a similar code, we have generated the 2-dimensional random arrays by declaring the size of the generation in two dimensions. For example, in variable ‘a’, we have declared the maximum limit to be 10 and the length of the sequence to be 3, 3, which is a two-dimensional array, and for ‘b,’ we have declared the maximum limit to be 50 and the length of the sequence to be 5, 5, and we got the corresponding 2-dimensional array as output.
We can also generate 3-dimensional arrays using the same technique as shown below.
Code:
from numpy import random
a = random.randint(10, size=(3,2,1))
print(a)
Output:
Code:
from numpy import random
b= random.randint(50, size=(5,2,1))
print(b)
Output:
In the above example, we have generated 3-dimensional random arrays for variables ‘a’ and ‘b’ where the size we have ‘a’ we have declared the maximum limit to be 10 and the length of the sequence to be (3, 2, 1), which is a two-dimensional array and for ‘b’ we have declared the maximum limit to be 50 and length of the sequence to be (5,2, 1) and we got the corresponding 3-dimensional array as output.
Example #4
In the previous example, we have discussed the generation of multiple dimensional arrays using integer values; here, we will discuss the generation of float values.
Code:
from numpy import random
a = random.rand(3,2)
b = random.rand(5,2)
print(a)
print(b)
Output:
When we are using random.rand syntax, it generates float values as default from 0 to 1, so when we declared the size of the random generation in two dimensions as we did in the above code where ‘a’ and ‘b’ is given a two-dimensional size of (3,2) & (5,2) we get the two-dimensional float array.
Similarly, for 3-dimensional float array,
Code:
from numpy import random
a = random.rand(3,2,2)
b = random.rand(5,2,2)
print(a)
print(b)
Output:
So in this code, we have declared the three-dimension size for a & b as (3,2,2) & (5,2,2), so we got the output in a three-dimensional array.
Example #5
Another method in Numpy random generation uses a function as a choice(), which allows the computer to make a random choice from the given sequence of values.
Code:
from numpy import random
a = random.choice([8, 10, 16])
b= random.choice([80, 100, 160,1100])
print(a)
print(b)
Output:
In this method, we use a function called choice(), which allows the computer to randomly select a value from the array of values the user declares.
The choice() function gets an array as input and gives a randomly selected value as output. Thus, we can give an array of any length and generate a random value from it.
Similarly, we can generate multi-dimensional arrays using the choice() function by giving the size of the dimension.
Code:
from numpy import random
a = random.choice([8, 10, 16],size=(3, 2))
b= random.choice([80, 100, 160,1100],size=(5, 2))
print(a)
print(b)
Output:
So using the choice() function, we can declare the size and generate 2-dimensional and 3-dimensional arrays from the array we have declared.
Conclusion
In this article, we have discussed Numpy random using various examples. We have also discussed generating random values using integers, floats, and arrays. We also discussed different techniques for generating multi-dimensional arrays numpy random can be very helpful for working on various projects involving random sequence generation.
Recommended Articles
We hope that this EDUCBA information on “NumPy random” was beneficial to you. You can view EDUCBA’s recommended articles for more information.