RandomNumGen - what is it and how to use it

I was browsing the list of classes in the Panda3d API reference and found this class: RandomNumGen. I understood that this is a random number generator. I also noticed a class named Mersenne. Are these two classes related? Does RandomNumGen use the Mersenne Twister algorithm to generate random numbers? If so, I would be interested to know what I need in order to use it, e.g. what modules to import and so on.

thanks

RandomNumGen uses Mersenne to help produce a random number. If you read the comments in the RandomNumGen, it seems that Disney created this class for generating random numbers for reasons linked to MMO gaming.
Looking at it, I would say Mersenne does use the Mersenne Twister algorithm, but I say that without any real authority.

to use it RandomNumGen, import the class from showbase:

from direct.showbase.RandomNumGen import RandomNumGen

I’m having problems importing RandomNumGen. I get an ImportError saying “cannot import name RandomNumGen.” When I tried to add “import direct.directbase.DirectStart” to the code I get another ImportError with the message “cannot import name DirectNotifyGlobal.” Do I need to import something else?

RandomNumGen is in /direct/src/showbase.
So I think you have to tipe


from direct.showbase.RandomNumGen import *

Then you can use RandomNumGen.

I think thats missing in the Reference. Josh is there a way to add it?

Martin

This doesn’t work either. With “import direct.directbase.DirectStart” I still get “cannot import name DirectNotifyGlobal” and without it I get “NameError: name ‘RandomNumGen’ is not defined.”

Hmm… thats strange.

On my computer thats works:

import direct.directbase.DirectStart
from direct.showbase.RandomNumGen import *

rn= RandomNumGen(5)
print rn.randint(4,5)

run()

Martin

Yes this is strange. What version of Panda are you using? I’m using 1.1.0.

Me too.

OK. I did some more experiments and it seems that the problem was really caused by the filename I was using for the random number program. I had named it “random.py” and when I changed the name to “rnd.py” everything worked. There’s a file named “random.py” in Panda3D-1.1.0\python\Lib folder and I think this may be the reason my program didn’t work with the name random.py.

Hmmm… very strange.
In which dir have you done your work?

I keep all of my own code in C:\Panda3D-1.1.0\mypanda. The code for the random number program is in mypanda\tests\ folder. I have tried putting my code into another folder which wasn’t even in the Panda3d folder but I still got the same error. Also, all of my other scripts and every Panda3d sample became unworking after I run the program as random.py. Deleting the compiled python file that was created and renaming the script to rnd.py fixed it. I’m glad that I got everything working. It restored my faith in Panda :wink: