[Ironruby-core] rand() needs a seed?

Curt Hagenlocher curth at microsoft.com
Sun Jul 6 11:15:41 EDT 2008


By default, the parameterless constructor of the System.Random class (which is what we're using) uses the system time as the initial seed.  Every time you ask for a random number, we create a new Random object, and in a tight loop like yours, they'll probably all get the same seed.

We should probably create a single Random object the first time that one is requested and store it in the RubyContext.

From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Johan Danforth
Sent: Sunday, July 06, 2008 7:28 AM
To: ironruby-core at rubyforge.org
Subject: [Ironruby-core] rand() needs a seed?

I'm a noob Ruby user, but IronRuby behaves different from other Ruby implementations that I've tried, in IronRuby (rev 121) this code:

100.times {p rand(100)}

Often returns the same value for (almost) all iterations, like this:

2
2
2
2
2
2
2
2
...and so on...

Works as designed? Need to seed it or something?

/Johan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20080706/a79e1752/attachment.html>


More information about the Ironruby-core mailing list