tags:

views:

782

answers:

5

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

Randomize lSeed
Response.Write Rnd

I'm noticing that it's returning the same result for two values in a row for lSeed (e.g. 123, 124) but then on say 125 it will return a new value but 126 will be the same as the on for 125. Why would this be?

Edit:

I have tried something like this

Randomize
Randomize lSeed
Response.write Rnd

And I get the same results I described above.

A: 

You should reseed before getting a random value each time. I'd recommend seeding to a timer.

FlySwat
I tried reseeding. I edited the question above to explain what I've tried.
Jon
A: 

you can also seed off of the current time that seems to work pretty well

jmein
I need to sometimes return consistent results so I need to reseed from a value I give it.
Jon
You probably shouldnt use Random numbers if you want consistent results.
Karl
Lol........... :)
FlySwat
If you provide the same seed you will get the same results from it.
Jon
A: 
' 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
Gordon Bell
This is an asp page and your code implies .Net.
Jon
Did you try it? Not that it solves your problem.
AnthonyWJones
OK, edited to include an ASP version...
Gordon Bell
A: 

The issue was that the value for the seed was too large in the other environment (an incrementing database value). I ended up doing value Mod 100 to get something semi-random and that was going to be low to always work.

Jon