2

So I have been playing around with linear congruential generators but I am stuck on the idea that once a script has been run, it always generates the same way. If I run a script such as this;

import random

print(random.randrange(0,9))

It will generate a new random number each time the script is run. I'm wondering how it does this.

So this is what I have gotten so far;

from collections import Counter

class linearCongruentialGenerator(object):
    def __init__(self, lowBound, highBound, length):
        self.lowBound, self.highBound, self.length = lowBound, highBound, length
        self.modulus = 20226231145#modulus
        self.multiplier = 104743412357321567#multiplier
        self.increment = 1049592131123467#increment
        self.seed = 43123211114#seed

    def __iter__(self):
        while self.length > 0:
            self.length -= 1
            self.seed = (self.multiplier*self.seed + self.increment) % self.modulus
            yield (self.seed % self.highBound)

    def linearCongruentialGenerator(self, first, last):
        self.seed = (self.multiplier*self.seed + self.increment) % self.modulus
        return self.seed

    def binary(self):
        self.seed = (self.multiplier*self.seed + self.increment) % self.modulus
        self.seedLength = len(str(seed))
        return (self.seed // 10**1 % 10) % 2

if __name__ == "__main__":
    random = linearCongruentialGenerator(0, 9, 100)
    random1 = linearCongruentialGenerator(0, 9, 100)
    list1 = []
    list2 = []
    for i in random:
        list1.append(i)
    for j in random1:
        list2.append(j)
    print(Counter(list1))
    print(len(set(list1)))
    print(Counter(list2))
    print(len(set(list2)))

This script counts the numbers generated by the linearCongruentialGenerator class then prints the distribution: number:count.

I want the script to generate new values every time I run the script. Without using the random class that comes with python. Because that is cheating :D

Not sure how to approach this, any tips?

5
  • 4
    Looks like you are hardcoding the seed instead of getting it from system noise. Commented Oct 7, 2018 at 9:36
  • 3
    Just do what we do to ensure this problem doesnt occur in random, set the seed as time.time() in init so it always seeds with the current time seed=time.time() Commented Oct 7, 2018 at 9:37
  • 1
    Oh that is cool! didn't think of that! Thanks Commented Oct 7, 2018 at 9:42
  • posting it as an answer, just mark it accepted so the issue is set as resolved :) Commented Oct 7, 2018 at 9:47
  • Actually just realised that it doesnt quite answer my question fully. If I were to print random.randrange(0,9) twice in the same script two different number will be generated, where as, if I did that it with the script I created it would use the same time and therefore the same seed generating the same numbers. time.time() will be the same when the script is run between all instance of the class Commented Oct 7, 2018 at 9:56

1 Answer 1

2

Just do what we do to ensure this problem doesn't occur in random, set the seed as time.time() in init so it always seeds with the current time seed=time.time()

Sign up to request clarification or add additional context in comments.

Comments

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.