You are on page 1of 8

Question 2: Generate random numbers with the help of mid-squared random number

generator.
Theory:
Implements von Neumann's middle-square method for generating 'pseudorandom' numbers
Essentially, take an l-digit number n, square it, and take the 'middle' l digits of the square.
Take the seed, square it, and just use the middle digits. To calculate the next number, you take
the previous random number and use that as the seed.
Objective:
To generate random numbers using mid squared random number generator

Algorithm:
1. Start with ‘N’ digit number
2. Square the ‘N’ digit number
3. Take ‘N’ digits in the middle from the squared result
4. Then square the new number from above to get the next number or seed
5. Repeat step 1 to 4 until you get the amount of random number you want

Program:
seed_number = int(input("Please enter a four-digit number:\n[####] "))
number = seed_number
already_seen = set()
counter = 0

while number not in already_seen:


counter += 1
already_seen.add(number)
number = int(str(number * number).zfill(8)[2:6]) # zfill adds padding of zeroes
print(f"#{counter}: {number}")

print(f"We began with {seed_number} and"


f" have repeated ourselves after {counter} steps"
f" with {number}.")
OUTPUT:

Conclusion:
Hence the random number was generated by implementing mid square method in python.

Question 3: Generate random number using congruential method or residual method.

Theory:
Linear Congruential Method is a class of Pseudo Random Number Generator (PRNG) algorithms
used for generating sequences of random-like numbers in a specific range. This method can be
defined as:
where,
X, is the sequence of pseudo-random numbers
m, ( > 0) the modulus
a, (0, m) the multiplier
c, (0, m) the increment
X0, [0, m) – Initial value of sequence known as seed
m, a, c, and X0 should be chosen appropriately to get a period almost equal to m.
For a = 1, it will be the additive congruence method.
For c = 0, it will be the multiplicative congruence method.
Objective:
To generate random numbers with the help of congruential method.

Algorithm:

1. Choose the seed value X 0, Modulus parameter m, Multiplier term a, and


increment term c.
2. Initialize the required amount of random numbers to generate (say, an
integer variable noOfRandomNums).
3. Define a storage to keep the generated random numbers (here, vector is
considered) of size noOfRandomNums.
4. Initialize the 0th index of the vector with the seed value.
5. For rest of the indexes follow the Linear Congruential Method to generate
the random numbers.
randomNums[i] = ((randomNums[i – 1] * a) + c) % m 
Finally, return the random numbers.
Below is the implementation of the above approach:

Program:

i. Linear Congruential generator

# Python3 implementation of the


# above approach

# Function to generate random numbers


def linearCongruentialMethod(Xo, m, a, c,
randomNums,
noOfRandomNums):
# Initialize the seed state
randomNums[0] = Xo

# Traverse to generate required


# numbers of random numbers
for i in range(1, noOfRandomNums):

# Follow the linear congruential method


randomNums[i] = ((randomNums[i - 1] * a) +
c) % m

# Driver Code
if __name__ == '__main__':

# Seed value
Xo = 5
# Modulus parameter
m=7
# Multiplier term
a=3
# Increment term
c=3
# Number of Random numbers
# to be generated
noOfRandomNums = 10

# To store random numbers


randomNums = [0] * (noOfRandomNums)

# Function Call
linearCongruentialMethod(Xo, m, a, c,
randomNums,
noOfRandomNums)
# Print the generated random numbers
for i in randomNums:
print(i, end = " ")

Output:

ii. For Multiplicative Congruential Generator


# Function to generate random numbers
def multiplicativeCongruentialMethod(Xo, m, a,
randomNums,
noOfRandomNums):
# Initialize the seed state
randomNums[0] = Xo

# Traverse to generate required


# numbers of random numbers
for i in range(1, noOfRandomNums):

# Follow the linear congruential method


randomNums[i] = (randomNums[i - 1] * a) % m

# Driver Code
if __name__ == '__main__':
# Seed value
Xo = 3
# Modulus parameter
m = 15
# Multiplier term
a=7

# Number of Random numbers


# to be generated
noOfRandomNums = 10

# To store random numbers


randomNums = [0] * (noOfRandomNums)

# Function Call
multiplicativeCongruentialMethod(Xo, m, a,
randomNums,
noOfRandomNums)
# Print the generated random numbers
for i in randomNums:
print(i, end = " ")

Output:

iii. For Additive Congruential Generator


# Function to generate random numbers
def additiveCongruentialMethod(Xo, m, c,
randomNums,
noOfRandomNums):

# Initialize the seed state


randomNums[0] = Xo

# Traverse to generate required


# numbers of random numbers
for i in range(1, noOfRandomNums):

# Follow the linear congruential method


randomNums[i] = (randomNums[i - 1] + c) % m

# Driver Code
if __name__ == '__main__':

# Seed value
Xo = 3
# Modulus parameter
m = 15
# Multiplier term
c=2
# Number of Random numbers
# to be generated
noOfRandomNums = 20

# To store random numbers


randomNums=[0] * (noOfRandomNums)

# Function Call
additiveCongruentialMethod(Xo, m, c,
randomNums,
noOfRandomNums)

# Print the generated random numbers


for i in randomNums:
print(i, end = " ")
Output:

Conclusion:
Hence we have implemented residual method in python to generate random number.
Question 4: Check the uniformity of the following random number with level of significance
(alpha=0.05) using KS test:

Ri=[0.24, 0.04, 0.89, 0.11, 0.61, 0.23, 0.86, 0.41, 0.64, 0.50, 0.65]

Theory:
It’s the test to check whether the given set of numbers are truly random or not. It is a test for
uniformity of the given set of random numbers. The KS test has a level of significance value
given and according to that the tabulated value of D is found and compared with the calculated
value we get after the calculations.
The Kolmogorov–Smirnov test can be modified to serve as a goodness of fit test. In the special
case of testing for normality of the distribution, samples are standardized and compared with a
standard normal distribution.
Objective:
Its main objective is to test the uniformity of random numbers.

Algorithm:
1. Sort the random numbers in ascending order.
2. Compute:
D+=max{(i/n)-ri} ; i<=i<=N
D-=max{ri-(i-1).N}
3. D=max(D+,D-)
4. Determine the critical value(alpha)
5. If D is greater then D alpha then data is not uniformly distributed else they are uniformly
distributed.

Program:
random_num = [0.24, 0.04, 0.89, 0.11, 0.61, 0.23, 0.86, 0.41, 0.64, 0.50, 0.65]
alpha = 0.05 # Given in the question
D_alpha = 0.391 # From the table
D_plus = []
D_minus = []
random_num.sort()
N = len(random_num)
print(N)
print("D+ \t\t D-")
for i in range(1, N+1):
D_plus.append(i/N - random_num[i-1])
D_minus.append(random_num[i-1] - (i-1)/N)
print(f"{D_plus[i-1]:.2} \t\t {D_minus[i-1]:.2} ")
D_plus = max(D_plus)
D_minus = max(D_minus)
D = max(D_plus, D_minus)
print("\n\nCalculated D is:", D)
print("AT aplha = ", alpha)
print("The Calculated D alpha from the table is:", D_alpha)

if D_alpha > D:
print("The given random_num is uniformly distributed")
else:
print("The given random_num is not uniform")

Output:

Conclusion:
Hence from the python program we have calculated that given random number is uniformly
distributed.

You might also like