ASP - Randomize函数为两个不同的种子返回相同的值

时间:2008-11-11 20:20:58

标签: asp-classic

所以我使用Rnd和Randomize生成一个随机数来设置一个如下所示的种子:

Randomize lSeed
Response.Write Rnd

我注意到它为lSeed(例如123,124)连续两行返回相同的结果,但是然后在125表示它将返回一个新值,但126将与125的on相同。为什么会这样?

编辑:

我尝试过这样的事情

Randomize
Randomize lSeed
Response.write Rnd

我得到了与上述相同的结果。

5 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

您应该在每次获取随机值之前重新设定。我建议播种定时器。

答案 2 :(得分:0)

你也可以从当前看起来效果很好的时间开始播种

答案 3 :(得分:0)

' 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

答案 4 :(得分:0)

问题是种子的值在其他环境中太大(递增的数据库值)。我最终做了价值Mod 100以获得半随机的东西,而且总是很低的。