The Best Seed for Random Numbers

  • Thread starter Thread starter Leon
  • Start date Start date
L

Leon

I know by default the random number generator use the time, but what is the
best seed I can used in my web application?

The Program generate 6 unique random numbers and load each of them in a
textbox control. I need a good seed like ip address or something.

'Function to generate random numbers

Public Function GetRandomNumber() As Integer

Dim objRandom As New System.Random

Return objRandom.Next(1, 26)

End Function
 
Leon said:
I know by default the random number generator use the time, but what is the
best seed I can used in my web application?

The Program generate 6 unique random numbers and load each of them in a
textbox control. I need a good seed like ip address or something.

'Function to generate random numbers

Public Function GetRandomNumber() As Integer

Dim objRandom As New System.Random

Return objRandom.Next(1, 26)

End Function
I don't know if this is best or not but I use (this is for a random number
in string format) Also you can change the 8 to a higher number for a larger
random number.

Dim strID as string = DateTime.Now.Ticks.tostring()
Dim rdm1 as Random
rdm1 = new Random(ctype(right(strID,8),int32))
strUnique = rdm1.next().tostring()
 
Why take ticks (long) and cast as a string only to cast it back to an
Integer?
Why take only the right 8 chars and not the whole value?

Dim myRandom As New Random(CType(Now.Ticks, Integer))

dim theNumber as Integer = myRandom.Next
 
Scott M. said:
Why take ticks (long) and cast as a string only to cast it back to an
Integer?
Why take only the right 8 chars and not the whole value?

Dim myRandom As New Random(CType(Now.Ticks, Integer))

dim theNumber as Integer = myRandom.Next
Well I do this because I wanted a 16 character string and later on in the
code (not included) I add the remaining characters. Also I think if you use
the whole tick it was too big for Random.
 
Scott M. said:
Why take ticks (long) and cast as a string only to cast it back to an
Integer?
Why take only the right 8 chars and not the whole value?

Dim myRandom As New Random(CType(Now.Ticks, Integer))

dim theNumber as Integer = myRandom.Next
Also, the help on Random says

However, if your application runs on a fast computer the system clock might
not have time to change between invocations of this constructor; the seed
value might be the same for different instances of Random. In that case,
apply an algorithm to differentiate the seed value in each invocation.
 
Well, you can take a look at the .NET RNGCryptoServiceProvider.
This example is taken right off of MSDN:

Dim random() As Byte = New Byte(100) {}

'RNGCryptoServiceProvider is an implementation of an RNG
Dim rng As New RNGCryptoServiceProvider()
rng.GetBytes(random) ' bytes in random are now random

Take a look at this, it may be what you were looking for
 
So there is definately a trade off then. This is the one thing I don't like
about technology...there is no perfect solution. There is a gotcha when
doing something. Well, I guess thats live in general.lol!
 
This code works great, but it looks wrong. What do you see?
'Function to generate random numbers

Public Function GetRandomNumber() As Integer

Dim random() As Byte = New Byte(100) {}

'RNGCryptoServiceProvider is an implementation of an RNG

Dim rng As New RNGCryptoServiceProvider

rng.GetBytes(random) ' bytes in random are now random

Dim objRandom As New System.Random(CInt(random(100)))

Return objRandom.Next(1, 26)

End Function
 
But casting to a string and taking the last 8 chars isn't going to change
the seed value if ticks is the same for 2 calls.

Also, by casting ticks to Integer (normally long), you are shrinking it down
to an acceptable seed value.
 
Scott M. said:
But casting to a string and taking the last 8 chars isn't going to change
the seed value if ticks is the same for 2 calls.

Also, by casting ticks to Integer (normally long), you are shrinking it down
to an acceptable seed value.
You are right. After I posted it I realized it didn't make sense. I did have
trouble with random when using the full tick and that is why I changed it. I
see your point.
 
Back
Top