4

I am trying to write a Linear congruential generator in python and I find a little piece of code on Wikipedia but have some difficulty on understanding it. The code is as follows:

def lcg(modulus, a, c, seed=None):
    if seed != None:
        lcg.previous = seed
    random_number = (lcg.previous * a + c) % modulus
    lcg.previous = random_number
    return random_number / modulus
lcg.previous = 2222

My problem is that what is "lcg.previous"? I notice that the function is done, the value of lcg.previous gets updated and stored. Is it declared as a member variable of function lcg() here or actually some kind of default set up for all function in python?

Thanks a lot!

7
  • Maybe you can give the reference of where you found that code snippet. Commented Apr 5, 2017 at 18:05
  • I guess you did not copy all the relevant code, and lcg.previous is set to a starting value directly after the function definition. Commented Apr 5, 2017 at 18:05
  • Sorry about this, I happen to upload the my edited version. Thanks for editing. Commented Apr 5, 2017 at 18:09
  • 1
    I wonder if this is very old code. A much better way of achieving this would be with generators, but maybe this was written before they were available. Commented Apr 5, 2017 at 18:14
  • @DanielRoseman yeah, it is a very old way to generate a uniform random variable Commented Apr 5, 2017 at 18:16

3 Answers 3

5

It is a "member variable" of the function, so that each time it is called (except when called with something for seed) the sequence will pick of where it left off.

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

2 Comments

+1. As a note, with this you can emulate the behaviour of static function variables common in other languages, like C.
Thanks for your explanation.
0

Python is recognizing the lcg.previous as a new variable declaration, and will add it as a member to lcg.

4 Comments

Python doesn't have variable declarations.
It does, it's just implied. the line <code>lcg(modulus, a, c, seed=None):</code> has implied local variable declaration of modulus, a, c, and seed. the line lcg.previous is an implied declaration of a member variable. Python just abstracts away the gritty part of declaring variables under the hood.
Python, the language, doesn't have a variable declarations. Variables spring into existence when they are assigned to. The language construct of variable declaration simply doesn't exist in Python, as it does in C, C++, Java etc. There is only assignment in Python. You are playing loose with terminology.
Variable declaration is either explicit or implicit. Most compilers interpret an assignment statement as purely an assignment of a value to a memory location. If there is no memory location associated with the reference, you'll at worst get a run time exception, or at best a compiler exception. Python sees that you are trying to use "seed", knows it doesn't have a memory address for it already, so it assigns one for you. Python operates under the assumption that your attempt to use a reference implies that you want the reference to actually exist.
0

The previous variable is a property on the lcg function. In this example it's being used as a static variable for the lcg function. Since Python doesn't require variables (or object members) to be declared before use you can create them at need. In this case, you've create a member previous of the lcg function object.

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.