Saturday, March 31, 2012

The Best Seed for Random Numbers

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" <vnality@dotnet.itags.org.msn.com> wrote in message
news:OxdtZQcxEHA.1404@dotnet.itags.org.TK2MSFTNGP11.phx.gbl...
> 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

>>
> 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()
"Scott M." <s-mar@dotnet.itags.org.nospam.nospam> wrote in message
news:%23VCC7gcxEHA.1396@dotnet.itags.org.tk2msftngp13.phx.gbl...
> 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
> >>
> > 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()
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." <s-mar@dotnet.itags.org.nospam.nospam> wrote in message
news:%23VCC7gcxEHA.1396@dotnet.itags.org.tk2msftngp13.phx.gbl...
> 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
> >>
> > 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()
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

"Leon" wrote:

> 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
>
Don't using the RNGCryptoServiceProvider slow down the web application?

"Tampa .NET Koder" <TampaNETKoder@dotnet.itags.org.discussions.microsoft.com> wrote in
message news:5E010EEC-665D-4FFA-8382-76DE85DF9C36@dotnet.itags.org.microsoft.com...
> 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
>
> "Leon" wrote:
>> 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
>>
>>
>
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!

"Leon" wrote:

> Don't using the RNGCryptoServiceProvider slow down the web application?
> "Tampa .NET Koder" <TampaNETKoder@dotnet.itags.org.discussions.microsoft.com> wrote in
> message news:5E010EEC-665D-4FFA-8382-76DE85DF9C36@dotnet.itags.org.microsoft.com...
> > 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
> > "Leon" wrote:
> >> 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
> >>
> >>
> >>
>
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

"Tampa .NET Koder" <TampaNETKoder@dotnet.itags.org.discussions.microsoft.com> wrote in
message news:9CA87134-C7C6-40F7-A51F-CBD0D1C98E6E@dotnet.itags.org.microsoft.com...
> 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!
> "Leon" wrote:
>> Don't using the RNGCryptoServiceProvider slow down the web application?
>>
>> "Tampa .NET Koder" <TampaNETKoder@dotnet.itags.org.discussions.microsoft.com> wrote in
>> message news:5E010EEC-665D-4FFA-8382-76DE85DF9C36@dotnet.itags.org.microsoft.com...
>> > 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
>>>>> > "Leon" wrote:
>>> >> 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
>> >>
>> >>
>> >>
>>
>>
>
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.

"vMike" <MichZaelY.GeZorgeY@dotnet.itags.org.noZorY.geZwaYrrenY.com> wrote in message
news:cmoi57$8f4$1@dotnet.itags.org.ngspool-d02.news.aol.com...
> "Scott M." <s-mar@dotnet.itags.org.nospam.nospam> wrote in message
> news:%23VCC7gcxEHA.1396@dotnet.itags.org.tk2msftngp13.phx.gbl...
>> 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
>>
>> >>
>> > 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()
>>>>
> 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.
>
>
"Scott M." <s-mar@dotnet.itags.org.nospam.nospam> wrote in message
news:eFB4dldxEHA.1988@dotnet.itags.org.TK2MSFTNGP12.phx.gbl...
> 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.
> "vMike" <MichZaelY.GeZorgeY@dotnet.itags.org.noZorY.geZwaYrrenY.com> wrote in message
> news:cmoi57$8f4$1@dotnet.itags.org.ngspool-d02.news.aol.com...
> > "Scott M." <s-mar@dotnet.itags.org.nospam.nospam> wrote in message
> > news:%23VCC7gcxEHA.1396@dotnet.itags.org.tk2msftngp13.phx.gbl...
> >> 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
> >>
> >> >>
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.

0 comments:

Post a Comment