The Random Number In Numpy

The random number in python is an algorithm that generates a random number that can not be predicted logically. This number can be integer or float, and it should be different every time you run the code. If the number generated by any program can be predicted, then you can’t call it random. 

Generating a random number is very important in many different situations, especially in encryption to protect data and scientific modeling. When you need to have some data for testing an algorithm or visualize the data, the list is endless.

1. Generate a Random Number

In python, there is a module inside the numpy library called random which will give you a random number. It has straightforward algorithms to generate random integers, floats, permutations, and distribution functions. Let’s now try to generate a random number:

from numpy import random

# Generate a Random Number
result = random.randint(40)

# Print The Random Number
print(result)

First, you need to import the random module from the numpy library, so you need to have numpy installed on your computer. Next, you will need to use the random.randint() function to generate the random number. 

We’ve set the value 40, meaning the number generated should be between 0–40, and store this value inside the result variable. Finally, print that number using the print function. You can notice that you will get another number less than the value you set every time you run the code.

2. Generate a Float Number

We’ve seen how you can generate a random integer number, but sometimes you need to have a float number. The random numpy module can make this with only one function. Let’s see how you can generate this number:

from numpy import random

# Generate a Random Number
result = random.rand()

# Print The Random Number
print(result)

You can notice we’ve created the same as the previous code, but we only changed the algorithm to random.rand() function will give you a random float from 0–1 every time you run the code.

3. Generate a Random Array

The previous two functions are used to generate random numbers, but we can use them also to generate random arrays with a slight change. We all know that numpy is the best library to work with the array available for python. Let’s generate a random array:

from numpy import random

# Generate a Random Array
result = random.randint(40, size=(6))

# Print The Random Array
print(result)

We’ve used the same previous code, but we’ve specified the size, which is how many random numbers you need inside the array. The value was 6, so we will get 6 values between 0–40. We could generate a random array of integer values, but what about the float numbers? Let’s see an example:

from numpy import random

# Generate a Random Array
result = random.rand(10)

# Print The Random Array
print(result)

You can specify a value inside the random.rand() function will give you many float numbers, basically an array. When you specify 10, you will have 10 float numbers as an array.

4. Change Array Shape

Numpy allows you easily to create an array with random values, but what we’ve done is generate a one-dimensional array. In some situations, you need arrays with many dimensions, such as 2-dimensional arrays. Let’s see how you can generate one with random values:

from numpy import random

# Generate a Random Array
result = random.randint(40, size=(6, 3))

# Print The Random Array
print(result)

We’ve generated 2-dimensional arrays with the same previous code with only one change, adding another number to the size parameter. The new generated array will have 6 rows and 3 columns with random values between 0–40. Let’s see now doing the same thing but with random arrays that have float values:

from numpy import random

# Generate a Random Array
result = random.rand(6, 3)

# Print The Random Array
print(result)

We’ve also used here the same code, but we specify how many rows, which is 6, and how many columns, which are 3, and it will create a 2-dimensional array with random float values.

5. Choose a Random Number

Numpy allows you to give a set of numbers as an array, and it will pick a number randomly from this set of numbers. Let’s see an example:

from numpy import random

# Choose a Random Number
result = random.choice([66, 12, 55, 46, 88])

# Print The Random Number
print(result)

You need to use the random.choice() function that will choose between a set of numbers inside the array and store the result inside a variable. You can give as many numbers as you need, but it should be only a 1-dimensional array because it will raise a ValueError telling you it must be 1-dimensional.

You can generate a random 2-dimensional array from a specific array that has a set of values using the same previous functions. Let’s see an example:

from numpy import random

# Generate a Random Array
result = random.choice([66, 12, 55, 46, 88], size=(6, 5))

# Print The Random Array
print(result)

The code will create a 2-dimensional array with 6 rows, so 6 different lists with 5 columns which mean 5 numbers in every list. These numbers will get them from the array you set inside the random.choice() function.

Conclusion

Thanks for reading! Numpy is one of the collect libraries you’ve ever seen, and it can work with arrays, generate random numbers, generate many different distributions, work with mathematical calculations, and even more. You can visit the official documentation to understand more about this library.

Scroll to Top