In: Computer Science
We will now simulate numpy.random(), which produces floating-point values in the range [0..1).
To do this, it is simply necessary to convert integers in the range [0..?)[0..m) returned by my_random_int() to floating point numbers in the range [0..1).[0..1).
Hint: Easily done by division!
# my_random returns a random floating-point number within [0, 1)
def my_random():
# your code here
return 0 # just to get it to compile
# Test it!
my_seed(0)
for x in range(0,10):
print(my_random())
CODE GIVEN:
a = 914334
m = 2**22 - 3
#a = 3 # You will use these in part (b)
#m = 7
def hash(x):
return (a * x) % m
# Test it!
X = [231,45,123,87,133,123]
for x in X:
print(hash(x))
next_value = 1 # just to create the variable
# seed next_value with the hash of (n+1)
def my_seed(n):
global next_value
next_value = hash(n+1) # so values do not start with seed and are
not 0
# my_random_int() returns a random number generated by the hash function, in the range [0..(m-1)].
def my_random_int():
global next_value
next_value = hash(next_value)
return next_value
# Test it
my_seed(0)
for x in range(10):
print(my_random_int())
python code
import numpy as np
# my_random returns a random floating-point number within (0, 1)
def my_random():
# return random float numbers
return np.random.uniform(0, 1)
# Test it!
#my_seed(0)
for x in range(0,10):
print(my_random())#print numbers
code &output:
//i have given the needed code alone, please do comments if you need any clarification or modification.