Sunday, February 18, 2007

Some More on Random Numbers

In a recent blog post, I wrote about random numbers and Powershell. As noted in that article, you can create a new random number object as follows:

$rand = New-Object  System.Random
$rand.next()
129641862

This approach to creating the random number object seeds the random number generator with a pseudo-random number (current time). Therefore, if you try this at home, you're not likely to see the same result to $rand.next(), i.e. 129641862.

If you wanted to create a repeatable random number sequence (which is of course not actually random!), you could add a seed number as follows:

$rand = New-Object  System.Random 1234567
$rand.next()
1673116976

If you try this code at home, you should get the same result to the call to $rand.next (i.e. 1673116976).

 

No comments: