Hi Born. Welcome to MacScripter.
The main problems, as you’ve realised, are choosing numbers which haven’t been chosen before (the most efficient way being to eliminate them from the source iist as they’re chosen) and arranging them in order when you’ve got them. AppleScript doesn’t have a sort command, but sorting’s possible by various means.
set the_values_list to {1, 4, 6, 8, 12, 14, 17, 19, 25, 27, 29, 31, 33, 38, 50}
set generated_choice to {}
set list_count to (count the_values_list)
repeat 6 times
set pick to (random number from 1 to list_count)
set end of generated_choice to item pick of the_values_list
-- Replace the integer in this position with a missing value.
set item pick of the_values_list to missing value
-- Get the remaining integers.
set the_values_list to the_values_list's integers
set list_count to list_count - 1
end repeat
sort(generated_choice)
return generated_choice
-- Simple insertion sort.
on sort(l)
repeat with i from 2 to (count l)
set v to item i of l
repeat with j from (i - 1) to 1 by -1
set w to item j of l
if (v < w) then
set item (j + 1) of l to w
else
set j to j + 1
exit repeat
end if
end repeat
set item j of l to v
end repeat
end sort
Edit: Unnecessary ‘here’ variable removed from the insertion sort. it was a hangover from a more efficient but more complex version of the code.
If the original numbers are already in order, as in your examples, another approach would be to choose all but six for elimination. The remaining numbers then wouldn’t need to be sorted:
set the_values_list to {1, 4, 6, 8, 12, 14, 17, 19, 25, 27, 29, 31, 33, 38, 50}
copy the_values_list to generated_choice
set list_count to (count generated_choice)
repeat while (list_count > 6)
set pick to (random number from 1 to list_count)
-- Replace the integer in this position with a missing value.
set item pick of generated_choice to missing value
-- Get the remaining integers.
set generated_choice to generated_choice's integers
set list_count to list_count - 1
end repeat
return generated_choice
But the “numbers” in your script are explicity text, so you’d need to use that class instead of ‘integer’ in the code:
set the_values_list to {"12", "14", "18", "21", "25", "27", "48", "67"}
copy the_values_list to generated_choice
set list_count to (count generated_choice)
repeat while (list_count > 6)
set pick to (random number from 1 to list_count)
-- Preplace the text in this position with a missing value
set item pick of generated_choice to missing value
-- Get the remaining texts.
set generated_choice to generated_choice's text
set list_count to list_count - 1
end repeat
return generated_choice
And if using the sort method with textual numbers, you’d not only have to change ‘integers’ to ‘text’ but put the sort() call in a ‘considering numeric strings’ statement to ensure that the sort is numeric rather than lexical:
considering numeric strings
sort(generated_choice)
end considering