Performance of random number list with AppleScript

Here is other performance test of random number in AppleScript.

Its like if we use random number from min to max have more instruction to be executed.
And if we use min + random number * (max - min). Its would be so useful if there was somehow a way to count execution to Apple Event. In this case the repeat loop become slower when the extra from 1.0 to 1000.0 was used with random number.

If we have random list of 1000 items we will save 10ms if we use min + (random number) * (max - min) instead of using random number from min to max. And the magnitude wil increase if the list if higher and 1000 items.

ex.

This is faster

set randomNumberList to {}
set {min, max} to {1.0, 1000.0}
repeat 10000 times
set end of randomNumberList to (min + (random number) * (max - min))
end repeat

VS.

This is slower

set randomNumberList to {}
repeat 10000 times
set end of randomNumberList to random number from 1.0 to 1000.0
end repeat

The less instructions in repeat loop the faster it become. When we use from N to N we use
range method and everytime we make a new item we execute this range again. And if we
have repeat loop that repeat 10000 times we also execute range 10000 times.

1. It looks to me when we use from 1 to 1000 we use integers.
2. random number make the integer to be real
3. convert back to integer
4. set/push to list

If we instead use from 1.0 to 1000.0 it will be faster and if we used integer.

In this case

set end of randomNumberList to (min + (random number) * (max - min))

We do not have to worry about any type casting becouse the input is already real. And maybe thats why its so much faster and AppleScript’s from 1 to 1000 (N is integer)

Hi @Fredrik71.

If you’re only interested in the comparative speeds of the random number generation methods, it would be better just to test the methods and leave out setting the ends of lists to the results. This eliminates any incidental variance in the list building speeds from the timings.

Ideally too, either both scripts should use the min and max variables within the repeat or both should have the reals in the repeat source code.

Theoretically, your faster method isn’t as random as the slower one because the greater the difference between max and min, and the higher the result from (random number), the further apart the possible values of (random number) * (max - min) will be. An alternative is (min + (random number (max - min))), perhaps with (max - min) calculated before the repeat. In the past, I’ve found this to be very slightly faster than using the from and to parameters. But there’s probably even less in it on today’s machines.

Thanks @Nigel_Garvey for your wise thoughts.

However, if you were to change the code above to this…

set randomNumberList to {}
set randomNumberListRef to a reference to randomNumberList
repeat 10000 times
set end of randomNumberListRef to random number from 1.0 to 1000.0
end repeat

The results from this version are almost identical to the speed of

set randomNumberList to {}
set {min, max} to {1.0, 1000.0}
repeat 10000 times
set end of randomNumberList to (min + (random number) * (max - min))
end repeat

After further testing, just adding the word my to the slower of the two original code samples… from this…

set randomNumberList to {}
repeat 10000 times
set end of randomNumberList to random number from 1.0 to 1000.0
end repeat

To this…

set randomNumberList to {}
repeat 10000 times
set end of my randomNumberList to (random number from 1.0 to 1000.0)
end repeat

Also made the speed comparisons almost identical.

@wch1zpink
Intresting… I have not thought about that before… to set current target could be so different compare to the slow version. And the example do not use any tell block so its also possible to use it.

set randomNumberList to {}
repeat 10000 times
set end of its randomNumberList to random number from 1.0 to 1000.0
end repeat