Seo is correct; in order to import random, you need the CPython standard 
library in sys.path. There are 3 ways to do this:

1.       Run IronPython from the standard library directory (the working 
directory is in sys.path by default)

2.       Append the standard lib directory to sys.path for invocation of 
IronPython, e.g.:
import sys
sys.path.append(r'c:\Program Files\IronPython 2.6\Lib')

3.       (Recommended) Set the environment variable IRONPYTHONPATH to point to 
the standard lib directory

System.Random is implemented in terms of .NET integers, which are 32-bit. When 
your script passed 9999999999, IronPython tried to represent it in 32 bits, 
causing an arithmetic overflow. In a pinch, you can use slightly more 
complicated logic to suit your needs, e.g.:
                var_utmn = randgen.Next(100000000,1000000000) * 10 + 
randgen.Next(9)
But using the CPython library is much cleaner :).

A final word of advice: Both standard libraries' random number generators use 
the convention that the first argument is inclusive and the second is 
exclusive. This means that your code will generate random numbers from 
1000000000 to 9999999998, which may or may not be what you want.

Good luck,
- David

From: users-boun...@lists.ironpython.com 
[mailto:users-boun...@lists.ironpython.com] On Behalf Of Adam Brand
Sent: Wednesday, June 24, 2009 5:22 PM
To: Discussion of IronPython
Subject: [IronPython] Random Number Generation

I feel newbish writing this, but I'm having problems generating random numbers 
in IronPython.

I tried "import random" but that doesn't seem to work (module not found).

I tried creating a System.Random but when I run
var_utmn = randgen.Next(1000000000,9999999999) I get a buffer overflow.

Any ideas? The random number needs to be above those two numbers indicated 
above.

Thanks,
Adam
_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to