In this python tutorial, you will learn about **Python NumPy Random**. Let us see how to generate random numbers in Python using NumPy.

In detail, we will cover the below topics with examples.

- What is a random number?
- Python NumPy random number
- How to generate a random float in Python
- Python NumPy random array
- Python NumPy random integer
- How to generate a random number from an array in Python
- Python NumPy random choice
- Python NumPy random randn
- Python NumPy random sample
- Python NumPy random uniform
- Python NumPy random number in the range
- Python NumPy random between two numbers
- Python NumPy random between 0 and 1
- python NumPy random number between 1 and 10
- Python numpy random seed
- Python numpy random choice
- Python numpy random integer
- Python numpy random normal
- Python numpy random uniform
- Python numpy random sample
- Python numpy random randn
- Python numpy random number generator
- Python numpy random exponential
- Python numpy.random.randomstate
- Python numpy random permutation
- Python numpy random shuffle
- Python numpy random binomial

**Table of Contents**show

## What is random number in python numpy?

**Random numbers** are the numbers that return a random integer. The random number does not mean a different number every time. Random means something that cannot be predicted logically.

Read: What is NumPy in Python

## Python numpy random

- In Python random is a module that is available in the NumPy library. This module returns an array of specified shapes and fills it with random floats and integers.
- It is based on pseudo-random number generation that means it is a mathematical way that generates a sequence of nearly random numbers
- Basically, it is a combination of a bit generator and a generator. In Python, the random values are produced by the generator and originate in a Bit generator.

**Syntax:**

Here is the Syntax of NumPy random

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

- It consists of only one parameter
**Size:**Default is None**Return:**In Python it will always returns a random integer or float numbers between the lower and higher limits.

**Examples:**

Let’s take an example and check how to implement random numbers in Python

```
import random
import numpy as np
result = np.random.randint(4)
print(result)
```

In the above code first, we will import a random module from the NumPy library. After that, I create a variable that is** ‘result’** and assign an **np. random()** function, and generate an integer number **‘4’.** Now use a print statement to check which number will be shown in the output.

Here is the execution of the following given code

## Python numpy random number

- Here we can see how to
**generate a random number in numpy Python**. - To obtain random numbers in Python we can easily use the
**randint()**function. - In Python, the numpy library provides a module called random that will help the user to generate a random number.
- In Python, the
**randint()**function always returns a random integer number between the lower and the higher limits these both limits are the parameters of the randint() function.

**Syntax:**

Here is the Syntax of **randint() **function

```
random.randint
(
low,
high=None,
size=None,
dtype=int
)
```

- It consists of few parameters
**low:**It establish the starting range and it takes only integer value as a parameter.**high:**It is an optional parameter and it shows the integer number to be drawn from the distribution.**dtype:**By default its value is int.**Out:**It explain the ending of the range.

**Example:**

Let’s take an example and check how to get a random number in Python numpy

**Source Code:**

```
import random
import numpy as np
new_out= random.randint(2,6)
print(new_out)
```

In the above code first, we will import a random module and then use the **randint()** function and to display the output use the print command it will show the number between **2 to 6.** As you can see my output the random number is **‘5’.**

Here is the Screenshot of the following given code

Let’s see another example on, how to **get a random number in python NumPy**. We can use the NumPy randint() method to generate a random number in Python.

```
from numpy import random
val = random.randint(50)
print(val)
```

You can refer to the below screenshot to see the output for **Python numpy random number**.

Read: Python NumPy Array

## Python generate a random float

Now, we will see **how to generate a random float in python**. We can use the **Python NumPy rand()** method to **generate a random float number in Python**.

```
from numpy import random
val = random.rand()
print(val)
```

You can refer to the below screenshot to see the output for **Python generate a random float**.

Read: Python NumPy Sum

## Python NumPy random array

Let us see, how to use **Python numpy random array** in python. We can use the randint() method with the Size parameter in NumPy to create a random array in Python.

```
from numpy import random
val = random.randint(50, size=(5))
print(val)
```

You can refer to the below screenshot to see the output for **Python numpy random array**.

Read: Python program to print element in an array

## Python NumPy random integer

Here, we will see **Python numpy random integer**. Below code, we can use the below code to create a random integer in Python NumPy.

```
from random import randrange
print(randrange(20))
```

You can refer to the below screenshot to see the output for **Python numpy random integer**.

## Python generate a random number from an array

Let’s see** how to generate a random number from an array **in python.

```
from numpy import random
val = random.choice([3, 5, 7, 9])
print(val)
```

You can refer to the below screenshot to see the output for **Python generate a random number from an array**

The above code, we can use to create a random number from an array in Python NumPy.

Read: Python concatenate arrays

## Python numpy random choice

Now, we will see **Python numpy random choice**.

```
import numpy as np
randm_num = np.random.choice(18)
print( "The random choice number : ")
print( randm_num )
```

You can refer to the below screenshot to see the output for** Python numpy random choice**

## Python NumPy random randn

Now, we will see **Python numpy random randn**, an example of creating a random number using the Python randn() method.

```
import numpy as np
random_num = np.random.randn(4)
print(random_num)
```

You can refer to the below screenshot to see the output for** Python numpy random randn**.

Read: Python Tkinter Grid

## Python NumPy random sample

**numpy.random.sample**() is one of the functions for doing random sampling in Python NumPy. Using .random sample() method. Here is an example of a random sample:

```
import numpy as np
random_num = np.random.random_sample(4)
print(random_num)
```

You can refer to the below screenshot to see the output for** Python numpy random sample**.

## Python NumPy random uniform

Now, we will use **Python NumPy random uniform**, it creates a NumPy** array that’s filled with numeric values**. Where size=0, low=1,high=10. These numeric values are drawn from within the specified range, specified by low to high.

```
import numpy as np
random_num = np.random.uniform(0,1,10)
print(random_num)
```

You can refer to the below screenshot to see the output for** Python numpy random uniform**.

Read: Python Tkinter OptionMenu

## Python NumPy random number in range

**Python NumPy random number in the range **is one function that can be generated random integers using the randint() function. It takes three arguments. Now let us give an example of a random range between (3,8).

```
import numpy as np
random_num = np.random.randint(3,size=8)
print(random_num)
```

You can refer to the below screenshot to see the output for** Python numpy random** **number in the range**

## Python NumPy random between two numbers

**Python NumPy random **is a function of the random module that is used to generate random integers numbers of type np.int between low and high where 3 is the lower value, 8 is high value and size is 10.

```
import numpy as np
random_num = np.random.randint(3,size=(8,10))
print(random_num)
```

You can refer to the below screenshot to see the output for** Python numpy random** **between two numbers**

Read: Convert string to float in Python

## Python NumPy random between 0 and 1

In this example, we will use the **NumPy np.random.seed() function** to show a random number between 0 and 1.

Random(3) specifies random numbers between 0 and 1 is the size of the keyword. Np.random.seed(number) sets what NumPy calls the global random seed.

```
import numpy as np
np.random.seed(32)
random_num = np.random.random(3)
print(random_num)
```

You can use the above code for** Python NumPy random** **between 0 and 1**.

## Python Numpy random number between 1 and 10

In this example, we will use the **NumPy randint()** function to generate a random number between 1 and 10.

```
import numpy as np
random_num = np.random.randint(1,10)
print(random_num)
```

The above Python code, we can use for** Python NumPy random between 1 and 10**.

## Python numpy random seed

- Let us see how to use the numpy random seed in Python.
- Numpy random seed is used to set the seed and to generate pseudo-random numbers. A pseudo-random number is a number that sorts random, but they are not really random.
- In Python, the seed value is the previous value number implement by the generator. If there is no previous value for the first time then it uses working system time.
- The main logic behind the random seed is to get the same set of random numbers for the given seed.

**Syntax:**

Here is the Syntax of numpy random seed

```
random.seed
(
self,
seed=None
)
```

**Examples:**

Here we will see how to execute the random number with the same seed value

```
import random
import numpy as np
np.random.seed(5)
new_val = np.random.randint(2,6)
print(new_val)
```

In this example, we use the** random. seed()** function and pass** ‘5**‘ as an argument. After that, I generate a random number between** 2 to 6.**

Here is the implementation of the following given code

## Python numpy random choice

- In Python to generate a random sample, we can use the concept of
**random. choice().**This function is available in the numpy library. - This function is commonly used in data science and data analytics.
- This method accepts four parameters and returns the random sample of the array. Random samples are very useful in data-related fields.

**Syntax:**

Here is the Syntax of numpy random choice

```
numpy.choice(
a,
size=None,
replace=True,
p=None
)
```

- It consists of few parameters:
**a:**numpy array and it contains numbers.**Size:**By default its value is None and output shape of array.**replace:**By default value is True and a value can be selected multiple times.

**Example:**

Let’s take an example and check how to generate a random sample by using the random choice() function

```
import numpy as np
new_rand = np.random.choice(15, 2)
print(new_rand)
```

Here is the Output of the following given code

## Another example to generate a uniform sample by using the random choice() function

**Source Code:**

```
import numpy as np
import matplotlib.pyplot as plt
new_samp = np.random.choice(16, 3000)
num, b, i = plt.hist(new_samp, 21, density = True)
plt.show()
```

Here is the Output of the following given code

## Python numpy random integer

- Let us see how to generate random integers in Python numpy.
- To create random integers we can easily use the
**randint()**function. This function returns a specified shape and it takes three arguments the lower, upper end of the range. - Random integers from the discrete uniform of the specified data type.

**Syntax:**

```
random.randint(
low,
high=None,
size=None,
dtype=int
)
```

**Example:**

Let’s take an example and check how to use random integers in Python numpy

```
import numpy as np
res_new = np.random.randint(low = 0, high = 4, size = 6)
print(res_new)
```

In the above code first, we will import the numpy library and then use the np. random() function. After that, we pass **low, high, and size** variables as an argument.

Here is the execution of the following given code

## Python numpy random normal

- Here we can see how to apply normal random in Python numpy.
- In this example, we can apply the concept of the numpy random
**normal()**function. This function enables you to declare a numpy array that stores normally distributed data. - The normal distribution is also called a curve because of its shape and size and these distributions can be used in data analysis and it is also a part of Gaussian distribution.

**Syntax:**

Here is the Syntax of random normal()

```
random.normal
(
loc=0.0,
Scale=1.0,
size=None
)
```

- It consists of few parameters:
**loc:**it’s an optional parameter and by default, its value is**0.0**and it represents the mean of the distribution.**Scale:**This parameter specifies the standard deviation of the distribution and by default, its value is**1**and it must be non-negative.**Size:**This parameter specifies the shape of a given array and the array can be**1-dimensional, 2-dimensional, or multi-dimensional.**

**Examples:**

Here we will discuss how to implement a random normal function in Python

```
import numpy as np
new_res= np.random.normal(size = 4)
print(new_res)
```

In the above code first, we will import a numpy library then we will use the concept of **random.normal()** function along with **size=4** as an argument and the output will display as the array of size** 5.**

Here is the execution of the following given code

## Another way to check how to use the random normal functions in Python

**Source Code:**

```
import numpy as np
from matplotlib import pyplot as plt
new_out = np.random.normal( size = 300 )
print( new_out )
num,x,y = plt.hist( new_out, 40)
plt.show()
```

Here we will use the **normal()** method of the random module. Now I want to display three hundred random sample numbers from the normal() function and pass **size=300** as an argument. In this example, I will also display the random sample in a plotting graph by using the **matplotlib **package.

**Output:**

As you can see output the sample number distribution shows a bell curve shape

## Python numpy random uniform

- Let us see how to use numpy random uniform in Python.
- The numpy random uniform function creates uniform distributed values and it will return the random sample as an array by using this function. In Python uniform distribution means it is a type of probability and prob always gives different outcomes.
- In this method, there is a range that we can declare by low and high.

**Syntax:**

Here is the Syntax of numpy random uniform

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

- It consists of a few parameters.
**low:**This parameter is the beginning point of the range and by default, its value is 0 and it will greater than or equal to low.**high:**This parameter is an endpoint of the range and the result value will be less than high and by default, its value is**1.0.****Size:**This parameter specifies the shape of a given array.

**Example:**

```
import numpy as np
new_result = np.random.uniform(0, 6, size=3)
print(new_result)
```

Here is the execution of the following given code

## Alternative way to check how to implement numpy random uniform function in Python

**Source Code:**

```
import numpy as np
import matplotlib.pyplot as plt
new_val = np.random.uniform(-4, 4, 3000)
plt.hist(new_val, bins = 40, density = True)
plt.show()
```

In this example, we can use np.** random.uniform() **method to get random samples from distributed values.

Here is the Screenshot of the following given code

## Python numpy random sample

- Here we can generate a numpy random sample in Python.
- In Python, the numpy module provides an np.random.sample() function for doing random sampling in the array. This method specifies the range of random float values as a one-dimensional array.
- It always returns an array of random floats within the range of
**[0.0,1.0).**

**Syntax:**

Here is the Syntax of the numpy random sample

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

**Source Code:**

```
import numpy as np
new_res_arr = np.random.sample(size =(2, 2))
print (new_res_arr)
```

In the above code, we have generated a** random. sample()** function in which we have pass size **(2,2)** as an argument.

**Output:**

## Python numpy random randn

- Let us see how to use Numpy random randn() function in Python.
- In Python, the
**random randn()**method creates a numpy array and returns a sample distribution. This method takes the shape of an array and fills it with random values. - This function returns all values in the distribution mean with float values.

**Syntax:**

Here is the Syntax of the numpy random randn() function

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

Here **do,d1,…dn** these are the optional parameter and it checks the condition if no parameter is given a single float is returned. The dimension of an array must be non-negative. This function generates a shape filled with float values.

**Examples:**

Let’s take an example and check how to use the numpy random randn() function in Python

```
import numpy as np
new_arr = np.random.randn(2, 3)
print(new_arr)
```

Here is the execution of the following given code

As you can see from the output the values are in the float form

Another example to create a three-dimensional array by using the random randn() function

**Source Code:**

```
import numpy as np
d_arr = np.random.randn(5, 3, 2)
print(d_arr)
d_arr2 = np.random.randn(3, 5, 3,4)
print("Multidimensional array",d_arr2)
```

You can refer to the below Screenshot to see the output

## Python numpy random number generator

- In Python, the generator provides entry to a wide range of normal distribution and is replaced with a random state.
- It is a mathematical way that generates a sequence of nearly random numbers and the generator relies on an additional bit generator to control random bit numbers. In Python the random values are produced by the generator and originate in a Bit generator.

**Syntax:**

Here is the Syntax of the numpy random generator

`numpy.random.default_rng()`

**Note:** By default, the bit generator takes a value**(PCG64)** and if you want to initialize a bit generator then use the seed parameter in it and it will return the initialized generator object.

**Example:**

```
import numpy as np
gen_new = np.random.default_rng(4567)
print(gen_new)
new_val = gen_new.random()
print(new_val)
```

Here is the execution of the following given code

## Python numpy random exponential

- Here we can see how to generate exponential random samples in Python.
- In Python the exponential distribution can get the sample and return numpy array.

The Probability Density function is

` f(x;1/β)= 1/βexp(-x/β)`

**Note:** x>0 and β is the parameter which is the inverse of the rate parameter λ=1/β

Syntax:

Here is the Syntax of numpy random **exponential**

```
random.exponential(
scale=1.0,
size=None
)
```

Example:

Here we will generate a random sample of exponential distribution by using the **random exponential()** method

```
import numpy as np
new_plot = np.random.exponential(23)
print(new_plot)
```

**Output:**

## Python numpy random.randomstate

- Here we will see how to access the randomstate method in the numpy random module.
- In Python, the randomstate provides seed to the random generator and it is used for the inheritance seeding algorithm and currently resets the state of
**MT19937**using the method of**randomstate().**

**Syntax:**

Here is the Syntax of the following given code

```
numpy.random.randomstate(
seed=None
)
```

In this function, the seed parameter initializes the pseudo number generator and can be an integer.

Example:

```
import numpy as np
new_plot = np.random.RandomState(4)
print(new_plot)
```

Here is the implementation of the following given code

## Python numpy random permutation

- Let us see how to use numpy permutation in Python. This method randomly generates a sequence and gets a randomly permuted range in Python.
- If x is a multidimensional numpy array and it is mix with the first index.

**Syntax:**

Here is the Syntax of numpy random permutation

`random.permutation(x)`

**Note:** Here x is an integer value and it randomly permutes and it always returns the random sequence array range.

**Example** of np. random.permutation

```
import numpy as np
new_output = np.random.permutation(6)
print(new_output)
```

In the above code first, we will take input x as **‘6’.** After that use **random.permutation()** function and get random sequence values.

Here is the execution of the following given code

## Python numpy random shuffle

- Here we can see how to use of numpy random shuffle in Python.
- In Python the shuffle means to arrange the objects and this method will help the user to modify the position of elements in a Numpy array.
- In this example, we will shuffle all the values in an array randomly.

**Syntax:**

Here is the Syntax of numpy random shuffle

`np.random.shuffle`

**Note:** Here X is the array or modifies sequence and it will return the shuffled array.

**Source Code:**

```
import numpy as np
new_array = np.arange(6)
np.random.shuffle(new_array)
print(new_array)
```

In this example, we have used the numpy function np.arange(). In Python, the np.arange() method creates a ndarray with spaced values within the interval or given limit. After that, we use the random shuffle function and pass the ‘new_array’ variable as an argument and print the result.

**Output:**

## Python numpy random binomial

- Let us see how to use a random binomial function in numpy Python.
- In Python, the binomial variables are a fixed number of trials and it returns two outcomes. Random samples are drawn from a distribution with given arguments.

**Syntax:**

```
np.random.binomial(
n,
p,
size=None
)
```

- It consists of few parameters
**n:**no of trails and distribution value is greater than equal to 0.**p:**This parameter is range between**>=0 and <=1****Size:**By default its value is none.

**Source Code:**

```
import numpy as np
import random
arr = np.random.binomial(n=8, p=0.5, size=15)
print(arr)
```

Here is the implementation of the following given code

You may like the following Python tutorials:

- Convert float to int Python + Examples
- Python NumPy zeros
- Python NumPy 3d array
- PdfFileMerger Python examples
- Append to a string Python + Examples
- Add string to list Python + Examples
- Python zip() Function Examples
- Check if NumPy Array is Empty in Python

In this Python tutorial, we learned with examples on **Python NumPy Random**:

- Python NumPy random number
- Python generate a random float
- Python NumPy random array
- Python NumPy random integer
- Python generate a random number from an array
- Python NumPy random choice
- Python NumPy random randn
- Python NumPy random sample
- Python NumPy random uniform
- Python NumPy random number in the range
- Python NumPy random between two numbers
- Python NumPy random between 0 and 1
- python NumPy random number between 1 and 10
- Python numpy random seed
- Python numpy random choice
- Python numpy random integer
- Python numpy random normal
- Python numpy random uniform
- Python numpy random sample
- Python numpy random randn
- Python numpy random number generator
- Python numpy random exponential
- Python numpy.random.randomstate
- Python numpy random permutation
- Python numpy random shuffle
- Python numpy random binomial

Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.