Random Number Generation

View: New views
5 Messages — Rating Filter:   Alert me  

Random Number Generation

by adambsk :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

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@...
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Re: Random Number Generation

by Seo Sanghyeon-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/6/25 Adam Brand <adamb@...>:
> I tried “import random” but that doesn’t seem to work (module not found).

Works for me. Do you have Python standard library in sys.path?

--
Seo Sanghyeon
_______________________________________________
Users mailing list
Users@...
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Re: Random Number Generation

by David DiCato :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

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 J.

 

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@... [mailto:users-bounces@...] 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@...
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Re: Random Number Generation

by adambsk :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

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@... [mailto:users-bounces@...] 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 J.

 

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@... [mailto:users-bounces@...] 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@...
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Re: Random Number Generation

by Lepisto, Stephen P :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

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@... [mailto:users-bounces@...] 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@... [mailto:users-bounces@...] 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 J.

 

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@... [mailto:users-bounces@...] 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@...
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com