(Not very) random numbers

I have a script that produces a random number between 1 and 1000, ie:

set theNumb to random number from 1 to 1000

However I find that this method doesn’t really produce very random numbers at all. What seems to happen is that around 99% or the numbers generated are from the same set of 8-10 numbers (ie 121, 319, 614…).

Is there a way to get these numbers to be a little more random?
TIA

I don’t have any scientific information about random numbers (it can be a highly technical topic), but why not generate several random numbers and then do some arithmetic on them to produce the final result. For instance:

set ranNumOne to random number from 1 to 1000 
set ranNumTwo to random number from 1 to 1000 
set ranNumThree to random number from 1 to 1000
set finalRanNum to round ((ranNumOne + ranNumTwo + ranNumThree) / 3)

This puts 3 random numbers into the mix instead of 1, and might produce more of the variety that you desire.

Run this script to view the statistic of the generated random numbers:

set lowest to 1000
set highest to 0
set theList to {}
repeat with i from 1 to 999
	set theList to theList & {0}
end repeat
repeat 1000 times
	set theNumb to random number from 1 to 999
	if theNumb is less than lowest then set lowest to theNumb
	if theNumb is greater than highest then set highest to theNumb
	set (item theNumb of theList) to (item theNumb of theList) + 1
end repeat
set generated to 0
repeat with i from 1 to 999
	if (item i of theList) is not equal to 0 then set generated to generated + 1
end repeat
display dialog " Lowest: " & lowest & return & ¬
	" Highest: " & highest & return & ¬
	" Generated: " & generated & " different numbers"

Cuneyt,

Thanks for that script, that’s useful, and it proves a point. Your script generated over 600 different numbers out of 1000 - pretty good going. That seems to be what happens when you do a repeat like that.

However my problem is that I just want a single number now and then, here’s what I’m getting so far, over a period of a couple of months:

28, 125, 399, 28, 571, 28, 609, 708, 87, 403, 664, 28, 664, 299, 125, 399, 28, 28, 571, 28, 399, 664, 125, 664, 664, 664, 664, 125, 125, 399, 664, 125

ie
28 * 7, 125 * 6, 399 * 4, 571 * 2, 609 * 1, 708 * 1, 87 * 1, 403 * 1, 664 * 6, 299 * 1

So out of 32 randoms, I’ve got 10 numbers, only 5 of which are unique, four of the remainder comprise three quarters of the numbers chosen. Doesn’t seem very random to me.

Another way to do this would be to read the system time in seconds and then mod it by a “random” number that AppleScript gives you. I believe you can get the system time in seconds since the last epoc and I would hope that AppleScript supports the mod operation. This should produce a fairly random number. In fact this is the same procedure that many library-based c and c++ random number generators use.

clive, i’ve no idea if you’ve fixed ur prob, but lemme give you some easy code to throw out random items that you’ve already used. it’s pretty painless and easy and what i use.

property used_list : {} property theNumb_List : {}
set used_list to {} set theNumb_List to {} repeat 10 times
set theNumb to random number from 1 to 1000
if used_list does not contain theNumb then
set the end of used_list to theNumb
set the end of theNumb_List to theNumb
end if
end repeat
return theNumb_List

easier than what i have seen so far frank

I think you could try to divide any two of these so called random numbers and then pick another random decimal place in the result. This looks more random and might be fun to build.