# Random function's maximum value is reached quickly

## Recommended Posts

Hello,

I just wanted to mention what I think is an interesting observation as I did some experiments

with the Random() in AutoIt.

I'm amazed at how quickly this returns a maximum value for Random()

What does this tell us about the Random function algorithm?

I like the algorithms results, ie nicely equally distributed, but I just find it

a bit curious that it should actually reach the exact maximum so quickly,

even when each time through the loop, the value of \$x becomes the new minimum value in Random()

I guess it says that the Random() algorithm is designed to reach it's maximum value sooner than

the maximum value within a reasonable amount of time for general usages of the Random()

Generally takes less than around 25 times through the loop

```\$x = 0
\$i = 0

While 1
;each time through the loop, the value of \$x becomes the new minimum value in Random()
\$x = Random(\$x, 1444000222, 1);1 billion, 444 million
\$i = \$i + 1
MsgBox(0, "" & \$i & " times through the loop", \$x, 1)
If \$x = 1444000222 Then
MsgBox(0, "" & \$i & " times through the loop", "reached maximum random value of " & \$x & "")
Exit
EndIf
WEnd```

Siimilar numbers of loops are needed to get to the maximum for 5000, interesting...

```\$x = 0
\$i = 0

While 1
;each time through the loop, the value of \$x becomes the new minimum value in Random()
\$x = Random(\$x, 5000, 1);only 5 thousand
\$i = \$i + 1
MsgBox(0, "" & \$i & " times through the loop", \$x, 1)
If \$x = 5000 Then
MsgBox(0, "" & \$i & " times through the loop", "reached maximum random value of " & \$x & "")
Exit
EndIf
WEnd```

frew

##### Share on other sites

I question your methods. The distribution looks pretty standard here:

```Global \$sMsg = "", \$iMax = 1000, \$iIter = 0

For \$i = 1 To 100
\$sMsg &= \$i
\$iIter = 0
While 1
\$iIter += 1
\$n = Random(1, \$iMax, 1)
If \$n = \$iMax Then
\$sMsg &= "; \$iMax (" & \$iMax & ") reached in " & \$iIter & " iterations" & @LF
ExitLoop
EndIf
WEnd
Next
ConsoleWrite(\$sMsg & @LF)```

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

##### Share on other sites

Thanks for your nice code PsaltyDS.

Yes, that shows the nice distribution very well, and how the Random() max is reached in a a way that is not more frequent than would be expected.

My code example was not to test the distribution results. I was just interested in how, when the Random() minimum value is increased each time through the loop, the Random() maximum seems to be reached so soon. I know increasing the Random() minimum value each time through increases the likelihood of getting to the Random() max sooner, but I was not expecting the Random() max to be arrived at so soon, even with the Random() minimum being increased each time through the loop.

Oh well, not that important really I guess. I was just doing some Random MouseClick tests that increment along the x axis, and noticed that the Random() max (the exact max number) seemed to be reached much sooner than I thought it would be...as in my code examples.

frew

## Create an account

Register a new account