Created
January 29, 2013 17:35
-
-
Save joelclermont/4666034 to your computer and use it in GitHub Desktop.
Putting random numbers in your random numbers and doing random byte math is really random, right? Wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Because we cannot use the default randomizer, which is based on the | |
' current time (it will produce the same "random" number within a | |
' second), we will use a random number generator to seed the | |
' randomizer. | |
' Use a 4-byte array to fill it with random bytes and convert it then | |
' to an integer value. | |
Dim randomBytes As Byte() = New Byte(3) {} | |
' Generate 4 random bytes. | |
Dim rng As New RNGCryptoServiceProvider() | |
rng.GetBytes(randomBytes) | |
' Convert 4 bytes into a 32-bit integer value. | |
Dim seed As Integer = (randomBytes(0) And &H7F) << 24 Or randomBytes(1) << 16 Or randomBytes(2) << 8 Or randomBytes(3) | |
' Now, this is real randomization. | |
Dim random As New Random(seed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By the way, they were afraid of collisions within a second, but instead generated an infinite recursion in the function that calls this nonsense.