Random Numbers in NumPy

M.Ramya

 What is a Random Number?

A random number is one that cannot be predicted logically.

It doesn’t just mean a different number every time—it means the result is unpredictable.

Pseudo Random vs. True Random

Computers follow instructions through programs. This means numbers generated by a computer are based on algorithms. As a result, random numbers generated by such algorithms can technically be predicted, making them pseudo-random.

Pseudo-random numbers are generated using formulas or algorithms and are not truly random, though they are sufficient for most tasks.

However, true random numbers can be generated by using data from unpredictable physical sources, such as:

Keyboard inputs

Mouse movements

Network traffic

These sources provide randomness that cannot be reproduced by a program.

True random numbers are generally needed in areas like:

Cryptography (for encryption keys)

Applications where absolute unpredictability is required (e.g., digital roulette wheels).

For most everyday uses, pseudo-random numbers are more than enough.

Generating Random Numbers with NumPy

NumPy has a random module for working with random numbers.

 Generate Random Integer

program:

Generate a random integer between 0 and 10

from numpy import random

x = random.randint(100)

print(x)

Output:

31

 Generate Random Float

Program:

Generate a random float between 0 and 1

from numpy import random

x = random.rand()

print(x)

Output:

0.681203824189047

Generating Random Arrays

Since NumPy works with arrays, you can easily create random arrays.

Random Integers

Program:

Generate a 1-D array with 5 random integers from 0 to 100:

from numpy import random

x = random.randint(100, size=(5))

print(x)

Output:

[9 90 31 34 77]

Program:

Generate a 2-D array with 3 rows and 5 random integers in each row:

from numpy import random

x = random.randint(100, size=(3, 5))

print(x)

Output:

[[80 54 19 74 65]
 [26 60 69 34 25]
 [50 16 53 84 90]]

Random Floats

Program:

Generate a 1-D array with 5 random floats:

from numpy import random

x = random.rand(5)

print(x)

Output:

[0.37454012 0.95071431 0.73199394 0.59865848 0.15601864]

Program:

Generate a 2-D array with 3 rows and 5 random floats in each row:

from numpy import random

x = random.rand(3, 5)

print(x)

Output:

[[0.58277988 0.1991148  0.04605748 0.96219602 0.08428238]
 [0.85002196 0.95423755 0.04370843 0.13380753 0.11908248]
 [0.34340898 0.34629577 0.87973959 0.05654732 0.87815835]]

Selecting Random Values from an Array

The choice() method allows you to randomly select values from a given array.

Program: 

Select One Random Value

Select one random value from an array:

from numpy import random

x = random.choice([3, 5, 7, 9])

print(x)

Output:

5

Program: 

Select Multiple Random Values

Create a 2-D array with random choices from a given list:

from numpy import random

x = random.choice([3, 5, 7, 9], size=(3, 5))

print(x)

Output:

[[5 9 7 5 9]
 [3 7 7 9 7]
 [3 7 9 9 5]]


Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send