random numbers in c#

D

david bandel

how do i make the random function be seeded by the timer? from the
complete lack of any resources on the web dealing with this i'm
assuming this is an extremely nontrivial problem. is there some magical
new technique for randomization that just makes no use of random
numbers? something specific to dot net.
 
N

Nicholas Paldino [.NET/C# MVP]

David,

You can just create a new instance of the Random class, with the default
constructor. It will seed the random number generator with the value
returned from the static TickCount method on the Environment class.

Hope this helps.
 
J

Jon Skeet [C# MVP]

david bandel said:
how do i make the random function be seeded by the timer? from the
complete lack of any resources on the web dealing with this i'm
assuming this is an extremely nontrivial problem. is there some magical
new technique for randomization that just makes no use of random
numbers? something specific to dot net.

No, I think there's a lack of resources on the web because it *is*
trivial :)

From the docs for the parameterless constructor for System.Random:
<quote>
Initializes a new instance of the Random class, using a time-dependent
default seed value.
</quote>

Alternatively, use the version which is given an int, and generate that
from the timer in whatever fashion you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top