codingstreets
Search
Close this search box.
python-numpy-random-numbers

Introduction to Python Numpy Random Numbers

In This Article, You Will Learn About Python Numpy Random Numbers

Python Numpy Random Numbers – Before moving ahead, let’s know a little bit about Python NumPy Filter Array

What is a Random Number?

A number is picked up from a group of numbers without following any set of instructions or making any logic.

Random Numbers on Computer Science

In everyday life, random numbers are chosen without any logic and reasons. They are just randomly selected among numbers. 

But in the technology world or computer science, sometimes numbers are not selected randomly; they are selected from a set of instructions (e.g., Algorithm) unless you provided a number from outside sources (Manual typed).

Pure Random Number

A number is a pure random number if a number is not selected or chosen on some set of instructions. More specifically, when numbers are provided from outside sources.

Generate Random Number (Integer)

To generate random numbers, a module random is available in NumPy to work with random numbers.

Example – Generating a random integer from 1 to 10.

from numpy import random

x = random.randint(10)

print(x)
python-numpy-random-numbers

As shown above, it returned a random integer from 1 to 10.

Generate Random Float

The random module provides a method rand() to generate a random float number.

Example – Generating a random float from 0 to 1.

from numpy import random

x = random.rand()

print(x)
Output - 

0.3547845

As a result, it returned a random float from 0 to 1.

Generate Random Array

We can use above mentioned methods to generate random arrays.

Integers

The randint() method takes as a parameter – size that allows specifying the shape of an array.

Example – Generating a 1-D array containing 4 random integers from 10 to 20.

from numpy import random

x=random.randint(20, size=(4))

print(x)
Output - 

[11 15 19 14]

As shown above, it returned a 1-D array containing 4 random integers from 10 to 20.

Example – Generating a 2-D array with 2 rows and 2 columns, each row containing 2 random integers from 10 to 20.

from numpy import random

x = random.randint(20, size=(2, 2))

print(x)
Output -

[[12 19]
   [18 13]]

As can be seen, it returned a 2-D array with 2 rows and 2 columns, each row containing 2 random integers from 10 to 20.

Float

The rand() method allows specifying the shape of an array.

Example – Generating a 1-D array containing 3 random floats.

from numpy import random

x = random.rand(3)

print(x)
Output - 

[0.33087409 0.69542496 0.4095021]

As had been noticed, it returned a 1-D array containing 3 random floats.

Example – Generating a 2-D array with two rows and two columns, each row containing two random numbers.

from numpy import random

x = random.rand(2, 2)

print(x)
Output - 

[[0.91230175 0.92506494]
 [0.59217842 0.65786051]]

As shown above, it returned a 2-D array with two rows and two columns, each row containing two random numbers.

The choice() method takes an array as a parameter and generates a random value based on an array.

Example – Taking out any random number from the array.

from numpy import random

x = random.choice([1, 2, 3, 4, 5, 6])

print(x)
Output -

5

As a result, it returned a single element from the array.

The choice() method takes as a parameter – size that allows specifying the shape of an array.

Example – Generating a 2-D array from the given array as a parameter.

from numpy import random

x = random.choice([11, 12, 18, 14], size=(2, 3))

print(x)
Output - 

[[11 12 14] 
 [14 11 18]]

As shown above, it returned a 2-D array from the given array.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on