Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > The Best Seed for Random Numbers

Reply
Thread Tools

The Best Seed for Random Numbers

 
 
Leon
Guest
Posts: n/a
 
      11-08-2004
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


 
Reply With Quote
 
 
 
 
vMike
Guest
Posts: n/a
 
      11-08-2004

"Leon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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,,int32))
strUnique = rdm1.next().tostring()


 
Reply With Quote
 
 
 
 
Scott M.
Guest
Posts: n/a
 
      11-08-2004
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,,int32))
> strUnique = rdm1.next().tostring()
>
>



 
Reply With Quote
 
vMike
Guest
Posts: n/a
 
      11-08-2004

"Scott M." <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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,,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.


 
Reply With Quote
 
vMike
Guest
Posts: n/a
 
      11-08-2004

"Scott M." <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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,,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.






 
Reply With Quote
 
=?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=
Guest
Posts: n/a
 
      11-08-2004
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
>
>
>

 
Reply With Quote
 
Leon
Guest
Posts: n/a
 
      11-08-2004
Don't using the RNGCryptoServiceProvider slow down the web application?

"Tampa .NET Koder" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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
>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=
Guest
Posts: n/a
 
      11-08-2004
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" <(E-Mail Removed)> wrote in
> message news:(E-Mail Removed)...
> > 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
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
Leon
Guest
Posts: n/a
 
      11-08-2004
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" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in
>> message news:(E-Mail Removed)...
>> > 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
>> >>
>> >>
>> >>

>>
>>
>>



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      11-08-2004
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" <(E-Mail Removed)> wrote in message
news:cmoi57$8f4$(E-Mail Removed)...
>
> "Scott M." <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> 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,,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.
>
>
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem this random seed() NZach Python 19 03-19-2013 03:26 PM
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random seed vivek7006@gmail.com Python 5 05-22-2009 12:16 AM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
random seed when using GSL Vumani Dlamini C Programming 1 04-21-2005 05:52 PM



Advertisments