Initializing a static variable in Python
Question: ## Context

Say, we want to use the Box-Muller algorithm. Starting from a couple of random
numbers U1 and U2, we can generate G1 and G2. Now, for any call, we just want
to output either G1 or G2. In other languages, we could use static variables
to be able to know if we need to generate a new couple. How can it be done in
Python?

## First thoughts

I know, much had been said about static variables in Python, I also must say
I'm a bit confused as the answers are quite scattered. I found this
[thread](http://stackoverflow.com/questions/592931/why-doesnt-python-have-
static-variables) and references therein. By that thread, the solution here
would be to use a generator.

## The devil's in the details

Now, the problem is, how do we initialize the generator and how do we retain,
say G2, for the next `yield`?


Answer: Writing generators is a very good practice!

    
    
    import random 
    import math
    def BoxMullerFormula(a,b):
        c = math.sqrt(-2 * math.log(a)) * math.cos(2 * math.pi * b)
        d = math.sqrt(-2 * math.log(a)) * math.sin(2 * math.pi * b)
        return (c,d)
    def BoxM(): 
         while 1:
            Rand01 = random.random()
            Rand02 = random.random()
            (a,b) = BoxMullerFormula(Rand01,Rand02)
            yield a 
            yield b 
    
    BoxM01 = BoxM()         
    for i in xrange(10):
        print BoxM01.next() 
    

You should consider also having a class BoxMuller with a get() method, a
re_initialize() one, and so on.

