Applescript arrays and random numbers

Hi,

I’m new to applescript and this maybe a really straightforward question. What i would like is an array of 15 random numbers between 1000 and 9999.

Any help would be appreciated.

Thanks
Joe

AppleScript has a random number generator:

set RandList to {}
repeat 15 times
	set end of RandList to random number from 1000 to 9999
end repeat
RandList

Of course this doesn’t sort them or assure that they are unique either. For unique numbers:

set RandList to {}
repeat until (count RandList) = 15
	set RN to random number from 1000 to 9999
	if RN is not in RandList then set end of RandList to RN
end repeat

And, building on Adam’s unique generator, a simple bubblesort will put them in order:

set RandList to {}
repeat until (count RandList) = 15
	set RN to random number from 1000 to 9999
	if RN is not in RandList then set end of RandList to RN
end repeat

set sortedRands to bubblesort(RandList)

on bubblesort(array)
	repeat with i from length of array to 2 by -1 --> go backwards
		repeat with j from 1 to i - 1 --> go forwards
			tell array
				if item j > item (j + 1) then
					set {item j, item (j + 1)} to {item (j + 1), item j} -- swap
				end if
			end tell
		end repeat
	end repeat
	return array
end bubblesort

Using this script, How could I get it pick the numbers again? but not choose the numbers previously picked?
however, I would like this to continue to pick the numbers eventually repeating numbers when it has ran out of numbers but only then picking those least chosen.

I’m looking at a way to pick some random numbers to but cover all possibilities in the lottery, so that you would win a prize since the odds of winning any prise are 1 in 52 (matching 3 numbers or more).

set RandList to {}
repeat until (count RandList) = 6
	set RN to random number from 1 to 49
	if RN is not in RandList then set end of RandList to RN
end repeat



set sortedRands to bubblesort(RandList)

log sortedRands



on bubblesort(array)
	repeat with i from length of array to 2 by -1 --> go backwards
		repeat with j from 1 to i - 1 --> go forwards
			tell array
				if item j > item (j + 1) then
					set {item j, item (j + 1)} to {item (j + 1), item j} -- swap
				end if
			end tell
		end repeat
	end repeat
	return array
end bubblesort

If it’s run out of numbers, they’ve all been chosen the same number of times. :wink:

Not quite sure if this does what you want. It randomly picks sets of six numbers for the number of times specified, using up each of the numbers from 1 to 49 before allowing them again. I believe it successfully avoids using the same number twice in any set, but there’s nothing in it (as it stands) to prevent equal sets.

set numberOfLinesRequired to 52 -- 52 sets of six numbers.

set numberSource to makeNumberSource({})
set theLines to {}

repeat numberOfLinesRequired times
	set randList to {}
	repeat 6 times
		set RN to some integer of numberSource
		set end of randList to RN
		set item RN of numberSource to missing value
		if ((count numberSource each integer) is 0) then set numberSource to makeNumberSource(randList)
	end repeat
	set end of theLines to bubblesort(randList)
end repeat

return theLines


-- Get a list of integers from 1 to 49, but substituting 'missing value' for any which are already in the line currently being processed above.
on makeNumberSource(currentRandList)
	set numberSource to {}
	repeat with i from 1 to 49
		if (i is in currentRandList) then
			set end of numberSource to missing value
		else
			set end of numberSource to i
		end if
	end repeat
	
	return numberSource
end makeNumberSource

on bubblesort(array)
	repeat with i from length of array to 2 by -1 --> go backwards
		repeat with j from 1 to i - 1 --> go forwards
			tell array
				if item j > item (j + 1) then
					set {item j, item (j + 1)} to {item (j + 1), item j} -- swap
				end if
			end tell
		end repeat
	end repeat
	
	return array
end bubblesort