I am generating random numbers and wat to disply them in rows with 6 nums in each row. Got the prog working so that I have an array of all my numbers, but cannot get it to format the output properly. Also I want to offer a hard printout option by pressing a button. Here is the code:
global vlu
global rcrd
set vlu to 0
set numbr to 0
set cownt to 0
set gamenum to {}
set niceprint to 1
set games to the text returned of (display dialog "How many games? " default answer 4)
repeat games times
set rcrd to {49, 49, 49, 49, 49, 49}
repeat with numbr from 1 to 6
randm()
set item numbr of rcrd to vlu
end repeat
set end of gamenum to rcrd
end repeat
*** My big problem is here ***
repeat games times
set gamenum to (items niceprint thru (niceprint + 5) of gamenum) & return & (items niceprint thru -1 of gamenum)
set niceprint to niceprint + 6
end repeat
display dialog gamenum as string
log gamenum
------generates and checks numbers against those already generated for uniqueness
on randm()
set answr to random number from 1 to 49
if answr ≠item 1 of rcrd and answr ≠item 2 of rcrd and answr ≠item 3 of rcrd and answr ≠item 4 of rcrd and answr ≠item 5 of rcrd and answr ≠item 6 of rcrd then
set vlu to answr
return
end if
randm()
end randm
set games to the text returned of (display dialog "How many games? " default answer 4)
set printOut to ""
repeat games times
set printOut to printOut & rand() & return
end repeat
display dialog printOut
to rand()
set lst to {}
repeat
set randomNumber to random number from 1 to 49
if randomNumber is not in lst then
set lst's end to randomNumber
if (count lst) is 6 then exit repeat
end if
end repeat
--> format (insert space between numbers)
set AppleScript's text item delimiters to space
set lst to lst as text
set AppleScript's text item delimiters to {""}
return lst
end rand
And here’s a “prettified” version built on jj’s that pads single digits and sorts each line in ascending order:
set games to the text returned of (display dialog "How many games? " default answer 5)
set printOut to ""
repeat games times
set printOut to printOut & rand() & return
end repeat
display dialog printOut
to rand()
set lst to {}
repeat
set randomNumber to random number from 1 to 49
if randomNumber is not in lst then
set lst's end to pad(randomNumber)
if (count lst) is 6 then exit repeat
end if
end repeat
--> format (insert 3 spaces between numbers)
set AppleScript's text item delimiters to space & space & space
set lst to UpSort(lst) as text
set AppleScript's text item delimiters to {""}
return lst
end rand
to UpSort(my_list) -- probably a prettier way to do this
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to ""
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end UpSort
to pad(num) -- adds a leading space to single digits so columns will line up
set thisNum to num as text
if length of thisNum = 1 then
set num to space & space & num
end if
return num
end pad
Thx to both jj and Nova Scotian for their solutions. One last point. How do I get my list of numbers to print on my hard copy printer? (It is network connected) PS: Nova Scotian’s solution is neat but does sometimes produce duplicates on each random selection of 6 numbers: eg. 3 3 15 23 34 46