1

I have a pseudorandom number generator (PRNG) with nice properties which uses six UInt32s as state. I need to come up with a reasonable way to seed it. Two obvious possibilities are: 1) generate six random numbers using System.Random and use them as seeds; 2) generate two GUIDs with Guid.NewGuid(). Which would be better?

I do not need cryptographic security.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487

4 Answers4

4

If it needs UInt32, then Random is more convenient? just Next(), Next(), Next() etc (and cast)... (use the same Random instance however - don't create new Random() each time).

It depends on what the intent is as to whether this offers enough randomness. Since this is just the seed, it should be OK...

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Wow, that was fast :) It just seems like way too little randomness, intuitively. But probably enough for my purposes. Should not get hung up on things like this. – Alexey Romanov Dec 15 '08 at 20:52
  • @alexey_r - then attach a radio antenna and use whitenoise to generate the randomness ;-p It has to come from somewhere... – Marc Gravell Dec 15 '08 at 20:54
  • Well, you won't get "more randomness" if you would generate random number of numbers, or seed it random number of times etc. If you use System.Random it will only as random as that is. And System.Random by defaults uses system tick count. If you don't need high security, this is enough. – lacop Dec 15 '08 at 20:55
  • I agree with Marc. Using a PRNG to seed another PRNG doesn't make anything any better or worse. – Jon B Dec 15 '08 at 21:01
  • Marc did not say anything positive about seeding a prng with another prng imo. It is a fact that randomness decreases if you do that - hence *not* creating a new Random each time due to how the value of ticks changes over time (I bet you would get a lot of repeat 'randomness') – John Nicholas May 15 '12 at 10:05
4

Unfortunately System.Random() also requires a seed value. By default it uses the current Tick count which is predictable and not actually random. So you'll need a seed for Random which leads you back to your original question ...

I haven't ever used Guid.GetHashCode() as a seed before but my 2 second reaction is that doesn't sound like a bad idea.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Is Guid.GetHashCode() any less predictable than the default seed? – Jon B Dec 15 '08 at 20:59
  • 1
    You'd need a crypto guy to get a definitive answer and I'm not one :) My guess is yes it's less predictable. Time is easily predicted. There are different GUID algorithms out there and if you know a few other pieces of data (hardware ID for instance) it is also predictable but it's a bit harder – JaredPar Dec 15 '08 at 21:10
  • I tried this actually the other day and it turned out that Guid's GetHashCode was terrible as a seed for System.Random. In fact so bad that within 3 cycles it actually turned out the exact same string of 6 random numbers !!! I was quite surprised but decided not to investigate further. – nbevans Dec 15 '08 at 21:24
  • 1
    @Nathan, how did you verify this? I just generated 10,000 GUIDs on my machine and got 0 dupes. Using powershell 1..10000 |%{[Guid]::NewGuid().GetHashCode() | select -unqiue – JaredPar Dec 15 '08 at 21:43
  • You will never get a duplicate Guid, but they are predictable and not a very good random seed. – Eric J. Sep 09 '09 at 00:30
  • @Eric J, there are circumstances under which you can get a duplicate GUID. Part of the uniqueness is by seeding the GUID with a known unique value such as a network card ID. Lacking a network card or other sufficient unique hardware ID, the GUID algorithm must fall back to chance and guessing which can produce duplicates under the correct circumstances – JaredPar Sep 09 '09 at 03:48
4

Whether or not you need cryptographic security, why not just use System.Security.Cryptography.RNGCryptoServiceProvider to generate your random numbers? Unless there's a specific reason, like it's too slow, I can't see why you wouldn't use it. Since it is a cryptographic random generator, you'll get much better random numbers, and don't have to be worried about seeding it.

Kibbee
  • 65,369
  • 27
  • 142
  • 182
-1

Try this for your seed value...

(UInt32)Math.Pow(System.DateTime.Now.TimeOfDay.TotalMilliseconds, 11.0 / 7.0)

It just raises the current time's milliseconds to the 11/7th power, which is just arbitrary. You can experiment with other fractions to see if they work better for you.

Beware that if your fraction's decimal equivalent is greater than about 2.5, you might get an overflow and your seed value will be zero. :(

I've used this for awhile and it seems to give pretty good seed values.

tvwxyz
  • 195
  • 3
  • 14