views:

1158

answers:

3

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

I do not need cryptographic security.

+4  A: 

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
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
@alexey_r - then attach a radio antenna and use whitenoise to generate the randomness ;-p It has to come from somewhere...
Marc Gravell
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
I agree with Marc. Using a PRNG to seed another PRNG doesn't make anything any better or worse.
Jon B
+3  A: 

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
Is Guid.GetHashCode() any less predictable than the default seed?
Jon B
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
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.
NathanE
@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
You will never get a duplicate Guid, but they are predictable and not a very good random seed.
Eric J.
@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
+1  A: 

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
Not a bad idea in general, but I need my RNG to be splittable, and `RNGCryptoServiceProvider` isn't.
Alexey Romanov