469,587 Members | 2,178 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,587 developers. It's quick & easy.

Trap when using Random() ... make sure it is not being changed tooquickly

For the public record.

RL

public void IterateOne()
{
Random myRandom = new Random(); //declare Random outside
the iteration
for (int j = 0; j < Max; j++)
{
// Random myRandom = new Random(); //donít declare
Random here since will not give random results if 'for' loopís too
fast!
}
}

//Declare your random variable once in your program. Any random
declared within the same time slice will have the same seed.
Oct 24 '08 #1
16 1805
Definitely. That is why the documentation for the System.Random()
constructor says: (http://msdn.microsoft.com/en-us/library/h343ddh9.aspx)

The default seed value is derived from the system clock and has finite
resolution. As a result, different Random objects that are created in close
succession by a call to the default constructor will have identical default
seed values and, therefore, will produce identical sets of random numbers.
This problem can be avoided by using a single Random object to generate all
random numbers.
"raylopez99" <ra********@yahoo.comwrote in message
news:bc**********************************@u46g2000 hsc.googlegroups.com...
For the public record.

RL

public void IterateOne()
{
Random myRandom = new Random(); //declare Random outside
the iteration
for (int j = 0; j < Max; j++)
{
// Random myRandom = new Random(); //donít declare
Random here since will not give random results if 'for' loopís too
fast!
}
}

//Declare your random variable once in your program. Any random
declared within the same time slice will have the same seed.

Oct 25 '08 #2
Family Tree Mike wrote:
Definitely. That is why the documentation for the System.Random()
constructor says: (http://msdn.microsoft.com/en-us/library/h343ddh9.aspx)

The default seed value is derived from the system clock and has finite
resolution. As a result, different Random objects that are created in
close succession by a call to the default constructor will have
identical default seed values and, therefore, will produce identical
sets of random numbers. This problem can be avoided by using a single
Random object to generate all random numbers.
It is something that is known for all languages and
technologies.

Arne
Oct 26 '08 #3
It is something that is known for all languages and
technologies.
I don't think that is quite accurate :-)

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com
Oct 26 '08 #4
Peter Morris wrote:
>It is something that is known for all languages and
technologies.

I don't think that is quite accurate :-)
I have seen it in C, Fortran, Java, Pascal, ASP/VBS
and PHP.

Arne
Oct 26 '08 #5
On Sun, 26 Oct 2008 02:41:29 -0700, Peter Morris
<mr*********@spamgmail.comwrote:
>It is something that is known for all languages and
technologies.

I don't think that is quite accurate :-)
Well, "all" might be overreaching. But "all broadly used" probably isn't,
especially if one is specifically discussing using the common technique of
seeding a random number generator with some time value, and especially if
we limit the discussion to Windows (where we know in advance the
limitations of retrieving a time value).

All pseudo-random-number generators require a seed, and the two most
common ways to provide that seed are to default to 0 and to use a time
value. In either case, repeatedly creating a new generator is always
wrong; to produce a series of random numbers, one should always initialize
the generator once, and then use the same one repeatedly.

Obviously if the seed is always the same, creating a new generator for
each new random number is wrong. When the seed is 0, that's a trivial
case to demonstrate. But it's also true that on the broadly-used PC
operating systems (including Windows), functions to retrieve the current
time have limited precision and if called too quickly in succession will
actually return the same value.

I think the most important lesson is that a person ought to read the
documentation when using some API. The caveat "FTM" quoted is provided in
multiple places in the documentation for Random, including the main class
page and (as he points out) the page documenting the constructor. Even a
person who wasn't familiar with the general issue would, given that
documentation, be able to learn about the specific caveats regarding the
Random class. Had they bothered to read the documentation, of course.

Which is a very long way of saying that Arne's comment might not have been
literally true, but I doubt that someone can come up with a
counter-example that involves a random-number generator in common use and
it's not like the issue described in this thread should be some sort of
remarkable revelation to anyone.

Pete
Oct 26 '08 #6
On Oct 24, 11:30*pm, raylopez99 <raylope...@yahoo.comwrote:
For the public record.

public void IterateOne()
* * * * {
* * * * * * Random myRandom = new Random(); //declare Random outside
the iteration
* * * * * * for (int j = 0; j < Max; j++)
* * * * * * {
* * * * * * * *// Random myRandom = new Random(); //donít declare
Random here since will not give random results if 'for' loopís too
fast!
* * * * * }

}

//Declare your random variable once in your program. *Any random
declared within the same time slice will have the same seed.
However, be careful of the fact that Random isn't thread-safe. That's
why I created the very simple StaticRandom class, which just creates
an instance of Random and makes every access to it sequential. It's
not particularly clever, but it's very useful :)

Jon
Oct 27 '08 #7
On Oct 24, 7:30 pm, raylopez99 <raylope...@yahoo.comwrote:
For the public record.

RL

public void IterateOne()
{
Random myRandom = new Random(); //declare Random outside
the iteration
for (int j = 0; j < Max; j++)
{
// Random myRandom = new Random(); //donít declare
Random here since will not give random results if 'for' loopís too
fast!
}

}

//Declare your random variable once in your program. Any random
declared within the same time slice will have the same seed.
You do realize that each time a new iteration is performed a new
instance of Random is create right?
As a component of Random is the time so of course it will ot work as
you expect.
Oct 27 '08 #8
On Oct 27, 7:17*am, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
On Oct 24, 7:30 pm, raylopez99 <raylope...@yahoo.comwrote:
For the public record.
RL
public void IterateOne()
* * * * {
* * * * * * Random myRandom = new Random(); //declare Random outside
the iteration
* * * * * * for (int j = 0; j < Max; j++)
* * * * * * {
* * * * * * * *// Random myRandom = new Random(); //donít declare
Random here since will not give random results if 'for' loopís too
fast!
* * * * * }
}
//Declare your random variable once in your program. *Any random
declared within the same time slice will have the same seed.

You do realize that each time a new iteration is performed a new
instance of Random is create right?
As a component of Random is the time so of course it will ot work as
you expect.
Not true. You probably did not notice that I //commented out the
code. The part that works is outside the loop.

Now tell me this: why does MSFT have two different strands of
Random()? One is supposedly more "cryptographically secure" or
robust. Perhaps the more robust version is 64 bits vs. 32 bits, or
something like that.

RL
Oct 28 '08 #9
On Oct 26, 10:13*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
Which is a very long way of saying that Arne's comment might not have been *
literally true, but I doubt that someone can come up with a *
counter-example that involves a random-number generator in common use and*
it's not like the issue described in this thread should be some sort of *
remarkable revelation to anyone.
Nothing in any language is 'remarkable', though pointers, delegates
and LINQ via lambda expressions come close. I am getting the hang of
using LINQ with lambda expressions, and haven't even bought a book on
the subject yet.

I posted this for future reference since Googling this topic only
yielded a hint in one other forum.

Thanks for your understanding.

RL

Oct 28 '08 #10
On Oct 27, 6:07*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
>
//Declare your random variable once in your program. *Any random
declared within the same time slice will have the same seed.

However, be careful of the fact that Random isn't thread-safe. That's
why I created the very simple StaticRandom class, which just creates
an instance of Random and makes every access to it sequential. It's
not particularly clever, but it's very useful :)

Hi,
What is a good book on parallel programming for C#? I have seen bits
and pieces here and there, and have sort of a 10000 m view, but if you
have any favorites please let me know.

RL

Oct 28 '08 #11
raylopez99 <ra********@yahoo.comwrote:
What is a good book on parallel programming for C#? I have seen bits
and pieces here and there, and have sort of a 10000 m view, but if you
have any favorites please let me know.
Coming very soon:

http://www.amazon.com/products/dp/032143482X

It's very, very detailed - be warned! (But wonderful...)

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 28 '08 #12
On Oct 28, 12:52*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
raylopez99 <raylope...@yahoo.comwrote:
What is a good book on parallel programming for C#? *I have seen bits
and pieces here and there, and have sort of a 10000 m view, but if you
have any favorites please let me know.

Coming very soon:

http://www.amazon.com/products/dp/032143482X

It's very, very detailed - be warned! (But wonderful...)

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
Thanks I pre-ordered it.

I hope it's also a tutorial too, not just a reference book.

RL
Oct 29 '08 #13
raylopez99 <ra********@yahoo.comwrote:
Thanks I pre-ordered it.

I hope it's also a tutorial too, not just a reference book.
I'm not sure I'd call it a tutorial as such, but it's far from just a
reference book.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 29 '08 #14
On Oct 29, 9:44*am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
raylopez99 <raylope...@yahoo.comwrote:
Thanks I pre-ordered it.
I hope it's also a tutorial too, not just a reference book.

I'm not sure I'd call it a tutorial as such, but it's far from just a
reference book.
I hope it's not one of those "philosophical" books that waxes
eloquently about the wonders of parallel programming without actually
showing much. BTW your book on advanced C# 3.0 is borderline like
this--it talks a lot about the history and evolution of C#3 from #2,
#1--which is OK but not really what I am looking for when learning the
language (after I've mastered it, it's another story). But your book
is useful and I'm reading it slowly everyday--your Appendix A on Linq,
like you've said, is very good. It's just now a keeper compared to,
say, Albahari et al's C# book, which is amazingly packed with
information. Still, for $20 bucks what you do expect? It's better
than nothing. I usually keep only half the books I buy--the rest I
read once and trash. I like your publisher's decision to make the
book available in keyword searchable PDF format, which means I can
index it using Google desktop and refer to key phrases later on.

RL
Oct 29 '08 #15
raylopez99 <ra********@yahoo.comwrote:
I hope it's also a tutorial too, not just a reference book.
I'm not sure I'd call it a tutorial as such, but it's far from just a
reference book.

I hope it's not one of those "philosophical" books that waxes
eloquently about the wonders of parallel programming without actually
showing much.
No, definitely not. You'll see a *lot* of detail.
BTW your book on advanced C# 3.0 is borderline like
this--it talks a lot about the history and evolution of C#3 from #2,
#1--which is OK but not really what I am looking for when learning the
language (after I've mastered it, it's another story). But your book
is useful and I'm reading it slowly everyday--your Appendix A on Linq,
like you've said, is very good. It's just now a keeper compared to,
say, Albahari et al's C# book, which is amazingly packed with
information. Still, for $20 bucks what you do expect? It's better
than nothing. I usually keep only half the books I buy--the rest I
read once and trash. I like your publisher's decision to make the
book available in keyword searchable PDF format, which means I can
index it using Google desktop and refer to key phrases later on.
Well, I'm glad you like Nutshell - so do I.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Nov 1 '08 #16
Jon Skeet [ C# MVP ] wrote:
>
Well, I'm glad you like Nutshell - so do I.
Yes, it's quite good. Every book has it's place. Some, like yours, I
read once to learn (though I have ripped out the Appendix A which I
use as a cheat sheet for Linq). Others, like Albahari's Nutshell, I
refer to daily. Others, like the receipe book and Judith Bishop's
Gang of Four design templates for C# book, you read once in a blue
moon just for solving a specific problem. Then you have specialty
books like that fat one on Linq that you reviewed recently--just to
drill down more. It's all good and cheap--where else can you get
expert instruction for just $20 or so? Even your colleagues cannot be
bothered for that cheap I would wager (plus you don't want to ask
them, since it might look like you don't know what you're doing).

I like this board too--a very good resource.

Back to coding....(I'm doing some graphics stuff ...which is full of
tricks....I'm developing a board game...)

RL
Nov 1 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

31 posts views Thread by lawrence | last post: by
11 posts views Thread by pemo | last post: by
10 posts views Thread by pemo | last post: by
4 posts views Thread by Christina | last post: by
17 posts views Thread by Army1987 | last post: by
39 posts views Thread by Alan Isaac | last post: by
94 posts views Thread by Samuel R. Neff | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.