# NumPy random number between two values in Python [3 Methods]

In this NumPy article, I will explain how NumPy random number between two values in Python through different methods with some illustrative examples. I will also explain what is numpy.random.rand() function in Python, and how it can be used to generate a random number between 0 and 1 values in Python.

To find the NumPy random number between two values in Python using the random module’s uniform function that will return uniformly distributed numbers between two values, the randint function will return random numbers, and the sample function will return a random float number.

## NumPy random number between two values in Python methods

There are three different methods to use the NumPy random to find the random number between two values in Python.

• numpy.random.uniform
• numpy.random.randint
• numpy.random.sample

Sure, let’s delve into the details of each of these three methods from the NumPy library in Python for generating random numbers, complete with examples.

## Method 1: NumPy random between two numbers using numpy.random.uniform

The numpy.random.uniform is used to generate random numbers from a uniform distribution in Python. In a uniform distribution, all numbers within the specified range are equally likely to be drawn.

Syntax:

``numpy.random.uniform(low=0.0, high=1.0, size=None)``

Example: Let’s generate 3 random floating-point numbers between 0 and 1 through Python NumPy.

``````import numpy as np

random_floats = np.random.uniform(low=0, high=1, size=3)
print(random_floats)``````

Output: This code will output three random numbers, each between 0 and 1 through np.random.uniform() in Python.

``[0.65899043 0.96190699 0.57745176]``

This way we can use the random.uniform() function for NumPy random number between two values in Python.

## Method 2: Python np.random.randint function

The numpy.random.randint is used to generate random integers from a discrete uniform distribution in Python. This means that any integer between the specified range is equally likely to be returned.

Syntax:

``numpy.random.randint(low, high=None, size=None, dtype='l')``

Example: Generating 4 random integers between 1 and 10 through the Python NumPy library.

``````import numpy as np

random_ints = np.random.randint(1, 11, size=4)
print(random_ints)``````

Output: This will output four random integers where each integer is between 1 and 10 using the randint() function in Python NumPy.

``[7 9 1 6]``

NumPy random number between two values in Python can be found using the random.randint() function.

## Method 3: NumPy random with sample() function

The numpy.random.sample is used to generate random floats in the half-open interval [0.0, 1.0). These numbers are drawn from a continuous uniform distribution over the stated interval in Python.

Syntax:

``numpy.random.sample(size=None)``

Example: Generating 5 random floating-point numbers between 0 and 1 through Python NumPy.

``````import numpy as np

random_samples = np.random.sample(size=5)
print(random_samples)``````

Output: This code will produce an array of 5 random numbers, each between 0 (inclusive) and 1 (exclusive) through the Python NumPy library.

``[0.82683374 0.55812677 0.8056722  0.56370462 0.40704623]``

This way we can use the random.sample() function in NumPy random number between two values in Python.

## The numpy.random.rand() function in Python

The np.random.rand() function in Python is a part of the NumPy library, which generates random numbers from a uniform distribution throughout [0, 1) in the form of an array in Python.

In other words, we can say this function generates random numbers(float values) between 0(included) and 1(excluded).

Syntax:

``numpy.random.rand(d0, d1, ..., dn)``

Example: Generating one number or two arrays of random numbers between 0 to 1 through the random.rand() function in Python.

``````import numpy as np

random_number = np.random.rand()
print("Single Random Number is:", random_number)

random_array = np.random.rand(5)
print("1D Array of  Random Number is:\n", random_array)

random_matrix = np.random.rand(3, 4)
print("2D Array of Random Number is:\n", random_matrix)``````

At first, we have not passed any argument to the function so it will return a random number.

In the second we have passed a single integer as an argument in the function, so this will create a one-dimensional array in Python.

At last, we have passed 2 integers as arguments in the np.random.rand() function, to get a 2D array in Python that will contain the number of random numbers as per described in the shape.

``````Single Random Number is: 0.43567479558792765
1D Array of  Random Number is:
[0.90554594 0.57586293 0.48271213 0.17694478 0.53889659]
2D Array of Random Number is:
[[0.0022054  0.84858505 0.43057461 0.08500639]
[0.10890669 0.49914648 0.04347735 0.03301075]
[0.01382021 0.72250946 0.4623091  0.1181017 ]]``````

## Conclusion

To find the NumPy random number between two values in Python, we can use the np.random.uniform(), numpy.random.randint(), and numpy.random.sample() function. All of them are explained in detail with some illustrative examples. Here I have also explained what the np.random.rand() function is, how it can be used to generate random numbers between 0 and 1 values in Python.

Now, the choice of the method depends upon the requirement of the code.

You may also like to read: