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

  • Thread starter Thread starter raylopez99
  • Start date Start date
R

raylopez99

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


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.
 
Family said:
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
 
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
 
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.
 
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
 
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
 
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
 
raylopez99 said:
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'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
 
raylopez99 said:
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 said:
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
 
Back
Top