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!

Jan Christoph Terasa
  • 5,781
  • 24
  • 34
Xuan
  • 163
  • 4
  • Maybe you can give the reference of where you found that code snippet. – Alex Apr 05 '17 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. – Jan Christoph Terasa Apr 05 '17 at 18:05
  • Sorry about this, I happen to upload the my edited version. Thanks for editing. – Xuan Apr 05 '17 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. – Daniel Roseman Apr 05 '17 at 18:14
  • @DanielRoseman yeah, it is a very old way to generate a uniform random variable – Xuan Apr 05 '17 at 18:16
  • What Python version is this? The code fails for me in both 2.7 and 3.5 -- and this is a neat language feature! – Prune Apr 05 '17 at 18:26
  • @Prune Sorry, you need to assign a value to lcg.previous first, I missed that line of code – Xuan Apr 05 '17 at 18:47

3 Answers3

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.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

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

Stephan
  • 666
  • 8
  • 23
  • Python doesn't have variable declarations. – juanpa.arrivillaga Apr 05 '17 at 18:12
  • It does, it's just implied. the line lcg(modulus, a, c, seed=None): 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. – Stephan Apr 05 '17 at 18:17
  • 1
    Python, the language, doesn't have a variable declarations. Variables [spring into existence](https://docs.python.org/3/tutorial/classes.html#instance-objects) 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. – juanpa.arrivillaga Apr 05 '17 at 18:24
  • 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. – Stephan Apr 05 '17 at 19:03
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.

John Percival Hackworth
  • 11,395
  • 2
  • 29
  • 38