1

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?

3
  • How are the rand8 numbers converted to rand11? Commented Nov 15, 2019 at 13:09
  • You can use the Inverse Integral Method to generate random variables. In this case you can generate numbers from a random variable that has Uniform Discrete distribution with parameter 11. Commented Nov 15, 2019 at 13:14
  • @CristiFati it is given in a file and I have to put this number into the formula Commented Nov 15, 2019 at 13:17

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

4 Comments

I'm not allowed to use random library
I explained it in the text, the random library is only used to show you that it generates a uniform distribution from a large input. You can remove it and use your seeds as input as in your question.
@MasoudMasoumiMoghadam Is this not what you wanted?
Just checking. please give me time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.