0

Python code

Can anyone help me in providing python code for random number generator without using random function

28th Nov 2016, 10:20 AM
Yudhishther Seth
Yudhishther Seth - avatar
3 Answers
+ 2
from time import time N = 2**32 M = 1664525 C = 1013904223 def rand(a, b): ''' Linear congruential random generator ''' random = hash(time()) while True: random = a + ((M*random + C) % N) % (b-a) yield random def test_rand(numtests, a, b): print('{0} random numbers in range({1}, {2}):'.format(numtests, a, b)) for i in range(numtests): print(next(rand(a, b))) if __name__ == '__main__': test_rand(10, 0, 100) Algo from here: https://en.wikipedia.org/wiki/Linear_congruential_generator
28th Nov 2016, 4:12 PM
donkeyhot
donkeyhot - avatar
+ 1
What numbers does it have to generate (integers, floats in range(a,b) etc.)? And why not using random module? Is it your home task? Or because of security issues and the generator has to use smth else instead of time for producing random numbers?
28th Nov 2016, 11:17 AM
donkeyhot
donkeyhot - avatar
0
yes its my assignment i have to generate integers please provide code and algo
28th Nov 2016, 11:34 AM
Yudhishther Seth
Yudhishther Seth - avatar