[IronPython] Random Number Generation

Lepisto, Stephen P stephen.p.lepisto at intel.com
Thu Jun 25 22:20:29 CEST 2009


If global.asax can set an environment variable, you can set (or append to) IRONPYTHONPATH to the path if interest.  When IronPython starts up, it takes the paths in IRONPYTHONPATH and adds them to the sys.path list.

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Adam Brand
Sent: Thursday, June 25, 2009 12:16 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Random Number Generation

Awesome, thanks that worked. I was using IronPython for ASP.net and did not have a pointer to the standard library.

One last question though if anyone knows...is there a way to do this sys.path.append from Global.asax? I know there is a python equivalent global.py, but we right now have a bunch of code in global.asax and would need to rewrite that in python if not. Or can they co-exist?

Thanks,
Adam

Adam Brand
SilverKey Technologies

From: users-bounces at lists.ironpython.com<mailto:users-bounces at lists.ironpython.com> [mailto:users-bounces at lists.ironpython.com<mailto:users-bounces at lists.ironpython.com>] On Behalf Of David DiCato
Sent: Wednesday, June 24, 2009 9:31 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Random Number Generation

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-bounces at lists.ironpython.com<mailto:users-bounces at lists.ironpython.com> [mailto:users-bounces at lists.ironpython.com<mailto:users-bounces at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20090625/7bdf6973/attachment.html>


More information about the Ironpython-users mailing list