Python NumPy Random + Examples

In this python tutorial, you will learn about Python NumPy Random and, also we will check:

  • 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

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 number

Let’s see how to get 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.

Python numpy random number
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.

Python generate a random float
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.

Python numpy random array
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 random interger 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 numpy random integer
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 array

Python generate a random number from array
Python generate a random number from 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 choice
Python numpy random choice

Python NumPy random randn

Now, we will see Python numpy random randn, an example of how to create a random number using 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.

Python NumPy random randn
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 sample
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.

Python NumPy random uniform
Python NumPy random uniform

Read: Python Tkinter OptionMenu

Python NumPy random number in range

Python NumPy random number in the range is one of the function can be generated random integers using 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 number in range
Python NumPy random number in range

Python NumPy random between two numbers

Python NumPy random is a function of random module 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

Python NumPy random between two numbers
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.

You may like following Python tutorials:

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