If you are really into statistical analysis you may be able to discuss at length about true random numbers and pseudo random numbers. Almost all random number generators are pseudo-random number generators.
In .NET you have two ways to generate Random numbers. The easiest is to use System.Random. This is the least random, but also the easiest to use. The other is to use the RNGCryptoProvider, which generates more random numbers, but is a lot more difficult to use.
The problem with System.Random, is that for a given seed, it always generates the same set of numbers. The problem is how to get a random enough seed to generate a sequence that is of value to you. Obviously the easiest is to use somekind of time related seed, say minutes or seconds or milliseconds. The issue with this is that the range of this is quite limited (ie. 0-60), but also that you may get the same seed if you have very iterative processing (ie. you run tight loops or multi-threaded apps).
So I just thought that maybe I can generate a GUID and use that as a seed. So I kind of came up with the following:
1 'Using the GUID to generate a seed may be more random than using the now.milliseconds.
2 Dim seed As Integer = Convert.ToInt32(Guid.NewGuid.ToString("N").Substring(1, 8), 16)
3 Dim rand As New System.Random(seed)
Then I did some searching and found a related post which uses the Hash of the GUID rather than the Guid itself to create a seed here: This is like, so random! Some Randomness from System.Random
There was also another recommendation on using something like this:
1 Dim seed As Integer = Now.Ticks Mod Int32.MaxValue
But I found that using this in a tight loop, I did get seeds that were the same. So though this may still work out for most cases, it is not as random as using the GUID.
Using the GUID is ofcourse a lot more complex operation on the processor and so may have an impact if you are generating a lot of random numbers.


Comments