[Zope-dev] product refresh and random number generation.

Romain Slootmaekers romain@zzict.com
Tue, 28 Jan 2003 12:31:36 +0100


hi,

I'm having problems with random number generation and product refreshes. 
To illustrate the problem, consider the following product code:

class RandomNumberGenerator(SimpleItem.SimpleItem):
     """
          ...
     """
     def __init__(self,id):
         "initialize an instance"
         self.id = id
         self.__random = Random(time.time())


     def next(self):
         """
              returns next random number.
         """
         return self.__random.random()


ok, this works but whenever the product is refreshed or the zope server 
restarts, the generator will start to generate the same sequence of 
numbers, which is not what I want.

I'm having trouble understanding what is happening:

since the object gets pumped up every request it would make sense
that you get the same random number if you lose some state from the 
internal random generator. But this isn't happening. You apparantly only 
lose state when the product is refreshed or when the zope server 
restarts. Could this be a cache effect ?

(I could, as a last resort seed the generator with the current time 
every request, but then, I lose all guarantees about the period for the 
random number generation)

any input appreciated.

Romain.