Imagine there is a rand8
function which returns a random number between [0 - 7]. Now I want to make a function namely as rand11
based on the input I get from rand8
. like this:
Input from rand8 : 1, 3 , 5 , 7 , 0 , 7 , 2 , 1 , 6 , ...
Output given by rand11: 0, 5 , 10 , 7 , 6 , 0 , 2 , 9 , 8 , ...
So far I found this online:
def lcg(modulus, a, c, seed):
while True:
seed = (a * seed + c) % modulus
yield seed
a = lcg(5, 0, 8, 1)
next(a)
But I am not familiar how to modify function to get numbers between 0 to 7 and return numbers between 0 to 10. Please note that I don't need to implement rand8
function. I only have to take care of rand11
function? And remember I am not allowed to use random
library in python or any other random libraries like numpy.random()
Can anyone help me?
This will give a list result
of the output you expect. As you can see if you ran this program it gives a uniform distribution of values between 0 and 10. The library random is only used to generate a larger list of rand8
seeds.
from random import randint
import collections
result = []
#seeds = [1, 3 , 5 , 7 , 0 , 7 , 2 , 1 , 6]
seeds = [randint(0,7) for i in range(1000000)]
gen = 1
for se in seeds:
gen = (se + gen) % 11
result.append(gen)
counter =collections.Counter(result)
print(counter)
If you want the function to have different results each time it runs, you can add a multiplier. Final code without showing the uniform distribution:
result = []
c = int(input("Seed?"))
seeds = [1, 3 , 5 , 7 , 0 , 7 , 2 , 1 , 6]
gen = 1
for se in seeds:
gen = (se * c + gen) % 11
result.append(gen)
print(result)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.