Professional Documents
Culture Documents
Question 2: Generate Random Numbers With The Help of Mid-Squared Random Number Generator. Theory
Question 2: Generate Random Numbers With The Help of Mid-Squared Random Number Generator. Theory
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
Conclusion:
Hence the random number was generated by implementing mid square method in python.
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:
Program:
# 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
# Function Call
linearCongruentialMethod(Xo, m, a, c,
randomNums,
noOfRandomNums)
# Print the generated random numbers
for i in randomNums:
print(i, end = " ")
Output:
# Driver Code
if __name__ == '__main__':
# Seed value
Xo = 3
# Modulus parameter
m = 15
# Multiplier term
a=7
# Function Call
multiplicativeCongruentialMethod(Xo, m, a,
randomNums,
noOfRandomNums)
# Print the generated random numbers
for i in randomNums:
print(i, end = " ")
Output:
# 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
# Function Call
additiveCongruentialMethod(Xo, m, c,
randomNums,
noOfRandomNums)
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.