Pseudorandom Number Generator using NumPy

 Pseudorandom Number Generator using NumPy

  • The pseudo-random number is a partial random number, not a ‘truly’ random number. These are computer-generated numbers (pre-determined) that look random.
  • These algorithms are a set of algorithms created by Computer Scientists to generate pseudo-random numbers (approximates).
  • Seed functions use for generate random numbers, based on “pseudo-random number generators” algorithms.

Syntax:-

random.seed()

example: -

import numpy as np 
np.random.seed(101) #Here, 101 is seed value 
np.random.randint(low = 1, high = 10, size = 10)

Output:

array([2,7,8,9,5,9,6,1,6,9])

  •  it shuffles data and initializes constants with random values. It is expressed as an integer, a floating-point number, a particular distribution, a defined range, and so on.

 Random Seed Importance

  • NumPy random () function based on some value called a seed value.
  • Numpy. random. seed () method initialized a Random State and generator is re-seeded.
  • The same seed value runs to the same random number generation even on different machines given the environment remains the same.
  • functions used to compute uniform, normal (Gaussian), lognormal, negative exponential, gamma, and beta distributions. For generating distributions of angles, the von Mises distribution is available.
  •  Table:- Partial list of numpy.random functions


Random Number Operations

1.    choice():- an inbuilt function returns a random item from a list, tuple, or string.
Example
# import random
import random
 # prints a random value from the list
list1 = [1, 2, 3, 4, 5, 6]
print(random.choice(list1))
 # prints a random item from the string
string = "striver"
print(random.choice(string)) 
Output:
 5
t

2. randrange(beg, end, step):- generate random numbers from a specified range.

Example:

# importing "random" for random operations

import random

# using choice() to generate a random number from a

# given list of numbers.

print("A random number from list is : ", end="")

print(random.choice([1, 4, 8, 10, 3]))

# using randrange() to generate in range from 20

# to 50. The last parameter 3 is step size to skip

# three numbers when selecting.

print("A random number from range is : ", end="")

print(random.randrange(20, 50, 3))

 Output

A random number from list is : 4

A random number from range is : 41

 3. random():-generate a float random number less than 1 and greater or equal to 0.


4. seed():- used to save the state of a random function and generate some random numbers on multiple executions of the code on the same machine or on different machines (for a specific seed value).

Example:

# importing "random" for random operations

import random

 # using random() to generate a random number

# between 0 and 1

print("A random number between 0 and 1 is : ", end="")

print(random.random())

# using seed() to seed a random number

random.seed(5)

# printing mapped random number

print("The mapped random number with 5 is : ", end="")

print(random.random())

# using seed() to seed different random number

random.seed(7)

# printing mapped random number

print("The mapped random number with 7 is : ", end="")

print(random.random())

# using seed() to seed to 5 again

random.seed(5)

# printing mapped random number

print("The mapped random number with 5 is : ", end="")

print(random.random())

# using seed() to seed to 7 again

random.seed(7)

# printing mapped random number

print("The mapped random number with 7 is : ", end="")

print(random.random())

Output
A random number between 0 and 1 is : 0.510721762520941
The mapped random number with 5 is : 0.6229016948897019
The mapped random number with 7 is : 0.32383276483316237
The mapped random number with 5 is : 0.6229016948897019
The mapped random number with 7 is : 0.32383276483316237

5. shuffle():- used to make a sequence (list). Shuffling means changing the position of the elements of the sequence.


Example:
# import the random module
import random
 # declare a list
sample_list = ['A', 'B', 'C', 'D', 'E']
 print("Original list : ")
print(sample_list)
 # first shuffle
random.shuffle(sample_list)
print("\nAfter the first shuffle : ")
print(sample_list)
 # second shuffle
random.shuffle(sample_list)
print("\nAfter the second shuffle : ")
print(sample_list)

Output:
Original list : 
['A', 'B', 'C', 'D', 'E']
After the first shuffle : 
['A', 'B', 'E', 'C', 'D']
After the second shuffle : 
['C', 'E', 'B', 'D', 'A']
 
6. uniform(a, b):- used to generate a floating-point random number between the numbers mentioned in its arguments. It takes two arguments, lower limit(included in generation) and upper limit(not included in generation).

Example:-
# importing "random" for random operations
import random
# Initializing list
li = [1, 4, 5, 10, 2]
# Printing list before shuffling
print("The list before shuffling is : ", end="")
for i in range(0, len(li)):
    print(li[i], end=" ")
print("\r")
# using shuffle() to shuffle the list
random.shuffle(li)
# Printing list after shuffling
print("The list after shuffling is : ", end="")
for i in range(0, len(li)):
    print(li[i], end=" ")
print("\r")
# using uniform() to generate random floating number in range
# prints number between 5 and 10
print("The random floating point number between 5 and 10 is : ", end="")
print(random.uniform(5, 10))

Output: 
The list before shuffling is : 1 4 5 10 2 
The list after shuffling is : 2 1 4 5 10 
The random floating-point number between 5 and 10 is : 5.183697823553464

Others:-
getrandbits() method — this allows randrange() to produce selections over an arbitrarily large range.
os.urandom() :- generate random numbers from sources provided by the operating system.
random.getstate():- Return an object capturing the current internal state of the generator. This is passed to setstate() to restore the state.
random.setstate(state):- state obtained from a previous call to getstate(), and setstate() restores the internal state of the generator.
==========================================================
=============================================

Post a Comment

0 Comments