The Best Seed for Random Numbers

Discussion in 'ASP .Net' started by Leon, Nov 8, 2004.

  1. Leon

    Leon Guest

    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, Nov 8, 2004
    #1
    1. Advertising

  2. Leon

    vMike Guest

    "Leon" <> wrote in message
    news:...
    > 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()
     
    vMike, Nov 8, 2004
    #2
    1. Advertising

  3. Leon

    Scott M. Guest

    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., Nov 8, 2004
    #3
  4. Leon

    vMike Guest

    "Scott M." <> wrote in message
    news:%...
    > 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.
     
    vMike, Nov 8, 2004
    #4
  5. Leon

    vMike Guest

    "Scott M." <> wrote in message
    news:%...
    > 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.
     
    vMike, Nov 8, 2004
    #5
  6. 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
    >
    >
    >
     
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=, Nov 8, 2004
    #6
  7. Leon

    Leon Guest

    Don't using the RNGCryptoServiceProvider slow down the web application?

    "Tampa .NET Koder" <> wrote in
    message news:...
    > 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
    >>
    >>
    >>
     
    Leon, Nov 8, 2004
    #7
  8. 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" <> wrote in
    > message news:...
    > > 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
    > >>
    > >>
    > >>

    >
    >
    >
     
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=, Nov 8, 2004
    #8
  9. Leon

    Leon Guest

    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" <> wrote in
    message news:...
    > 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" <> wrote in
    >> message news:...
    >> > 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
    >> >>
    >> >>
    >> >>

    >>
    >>
    >>
     
    Leon, Nov 8, 2004
    #9
  10. Leon

    Scott M. Guest

    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" <> wrote in message
    news:cmoi57$8f4$...
    >
    > "Scott M." <> wrote in message
    > news:%...
    >> 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., Nov 8, 2004
    #10
  11. Leon

    vMike Guest

    "Scott M." <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:cmoi57$8f4$...
    > >
    > > "Scott M." <> wrote in message
    > > news:%...
    > >> 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.
     
    vMike, Nov 8, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. random seed

    , Aug 16, 2005, in forum: Python
    Replies:
    5
    Views:
    5,351
    anil_2009
    May 22, 2009
  2. Vumani Dlamini

    random seed when using GSL

    Vumani Dlamini, Apr 21, 2005, in forum: C Programming
    Replies:
    1
    Views:
    1,062
    Ben Pfaff
    Apr 21, 2005
  3. globalrev
    Replies:
    4
    Views:
    796
    Gabriel Genellina
    Apr 20, 2008
  4. NZach

    Problem this random seed()

    NZach, Mar 19, 2013, in forum: Python
    Replies:
    19
    Views:
    167
    NZach
    Mar 19, 2013
  5. Nick Mellor
    Replies:
    7
    Views:
    117
    John Gordon
    Apr 16, 2014
Loading...

Share This Page