2015-11-08 54 views
0

如何獲得在範圍(0123)的隨機數(5)VBSCRIPT隨機帶一步一步

做得到範圍內的隨機數(0123)我做

Dim max,min 
max=123 
min=0 
Randomize 
response.write(Int((max-min+1)*Rnd+min)) 

但我只需要steped 5 (0,5,10,15,20,...)

回答

1
Function StepRandom(ByVal minValue, ByVal maxValue, ByVal Steps) 
    maxValue = maxValue - maxValue Mod Steps 
    Randomize 
    StepRandom = maxValue-Fix((maxValue - minValue + 1)*Rnd/Steps)*Steps 
End Function 

Response.Write CStr(StepRandom(0,120,5)) 

的基本操作是到最大值調整到正確的步驟點和從該最大值在上述範圍內計算下一個步驟點

1

我想,首先,獲得真正的最小值和最大值:

If min Mod 5 > 0 Then 
    min = min + (5 - (min Mod 5)) 
End If 
max = max - (max Mod 5) 

然後,得到在0和​​(最大值 - 最小值)/ 5之間的隨機數,乘以5,並添加分鐘。

Dim number 
number = Int(((((max - min)/5) + 1) * Rnd) * 5 + min)