ASP - Randomize function returns the same value for two different seeds

So, I am generating a random number using Rnd and Randomize to set a seed that looks like this:

Randomize lSeed
Response.Write Rnd

      

I notice that it returns the same result for two values ​​in a row for lSeed (e.g. 123, 124), but then say 125, it will return a new value, but 126 will be the same as for 125. Why should this be ?

Edit:

I've tried something like this

Randomize
Randomize lSeed
Response.write Rnd

      

And I get the same results as I described above.

0


source to share


5 answers


The problem was that the value for the seed was too high in another environment (database increment value). I ended up doing a Mod value of 100 to get something semi-random and that would be low to always work.



0


source


What's the problem with random numbers ...



http://web.archive.org/web/20011027002011/http://dilbert.com/comics/dilbert/archive/images/dilbert2001182781025.gif

+2


source


You must repeat the query until you get a random value every time. I would recommend planting a timer.

0


source


you can also strip out the current time, which seems to work pretty well

0


source


' For ASP, you can create a function like:
Public Function RandRange(ByVal low As Integer, ByVal high As Integer) As Integer
    Randomize()
    Return ((Rnd() * (high - low)) + low)
End Function

' For ASP.NET, you can create a function like:
Private _rnd As System.Random()
Public Function RandRange(ByVal low As Integer, ByVal high As Integer) As Integer
    ' Purpose: Returns Random Integer between low and high, inclusive
    ' Note: _rnd variable must be defined outside of RandRange function
    If _rnd Is Nothing Then
        _rnd = New System.Random()
    End If
    Return _rnd.Next(low, high)
End Function

      

0


source







All Articles