simple MOVE script

I have written a script that quizzes me at random from a tab-delimited dictionary of about 2000 lines and growing. I use the “random generator” command from Standard Additions; I supply only the “upper limit” parameter equal to the number of paragraphs in the file.

Two questions:

What is the best way for my needs to use this command?

Is there a random number generator OSAX I should be using instead?

There was a long discussion some time ago at Apple’s AUL about random number generators. Take a look here:
But I think your way is rigth:

set thisList to {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
thisList's item (random number from 1 to thisList's length)

You can also use “some item”. Here is a little script that will ask you questions from your list in random order only asking each question once:


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Thanks jj and jon.

jj: I will join the email list at Apple so I can read the discussion.

jon: I wasn’t aware of the “some item” technique. I actually considered something similar. Keeping a record of lines already tested in a property list and comparing each random number to the list before using it. I have noticed a relatively high frequency of repeated numbers between sessions.

How’s this for a plan?

Use a seed number and bump it by one with every random number generation and store the last used seed number in a script property.

If you want to avoid repetitions from one session to the next, you have to save something in a file, because as I understand it, that’s the only way to have data persist from one session to the next. If it’s simple, like a text string or number, writing it to a text file is probably easiest. If it’s more complicated data, you can save it in a script file. I don’t know the details of that, but they’re in “AppleScript: the Definitive Guide” by Matt Neuburg, O’Reilly publishers. That may be what the previous responder meant by “store it in a script property.”

Guaranteeing to use a different seed each time will probably reduce the likelihood of repeats. (I just looked up seed, and it’s not what I’m used to, so I don’t know what it will do.) If you want to eliminate repeats, save your used_list and read it back in next time. That has a limited life, though; every once in a while you would have to clear out or shorten the used_list because finding words that haven’t been used yet gets harder and harder, and of course, impossible once you’ve used the whole list.

In a sense, the most elegant method is to avoid repeats in the first place. One way to do that is by deleting an item once it’s been used. In other words, iinstead of keeping a used-list, you keep an unused_list, which is more time efficient. Read the original word list into a variable (you may already be doing that), and whenever you’ve used a word, delete it from the variable, so that the unused_list is shorter by one. Keep that variable in between sessions by saving it in a script file, and keep using it from session to session until it gets too short, then read in the original word list from its file and start over.

If that method takes up too much memory, there are ways of addressing that.

These are things I’ve done in other languages. I don’t have a lot of experience with AppleScript. I may try them out in AS and report back.


I’m not sure if this will help any, but Cian Phillips and I came up with this a long time ago…

global vlu
global rcrd
set vlu to 0
set numbr to 0
set cownt to 0

	set rcrd to {55, 55, 55, 55, 55, 55}
	repeat with numbr from 1 to 6
		set item numbr of rcrd to vlu
	end repeat
	display dialog " Here Are Your Lottery Numbers: " & return & " 
          " & item 1 of rcrd & " , " & item 2 of rcrd & " , " & item 3 of rcrd & " , " & item 4 of rcrd & " , " & item 5 of rcrd & " and " & item 6 of rcrd buttons {"End", "Try Again"} default button 2
	if button returned of result is "End" then exit repeat
end repeat

------generates and checks numbers against those already generated for uniqueness 

on randm()
	set answr to random number from 0 to 55
	if answr is not item 1 of rcrd and answr is not item 2 of rcrd and answr is not item 3 of rcrd and answr is not item 4 of rcrd and answr is not item 5 of rcrd and answr is not item 6 of rcrd then
		set vlu to answr
	end if
end randm

Jim, while data is not persistent in properties within AppleScript Studio applications, this isn’t the case with AppleScripts saved as applications or scripts from the Script Editor. For these types of scripts, you can use properties that are persistent through relaunches. The persistence ends either when the script is recompiled or through an explicit call within the script itself to reinitialize the properties.


What says Jon is true, unless you are running the script in a special environment (eg FastScripts, or a datafork script from Entourage’s script menu) or your script (applet/droplet) is not writeable (permissions won’t allow nobody to store changes).
Thought I should quote this from a great current discussion at the MACSCRPT list about persistence of globals (and also properties).

I found that on further reading, Jonn, thanks. Saving or storing seems to take care of all cases, but probably using persistence of an applet’s properties would be good enough for the present case.

I found a strange thing by experimentation – I could “store script” to a file I had created with “store script,” but not to one that I had typed in and saved myself. Trying to store to one I had created by hand failed silently, no error message, it just didn’t do anything. I suppose it’s a matter of permissions. Sometimes AS seems like a trackless swamp. An error message might have helped. It didn’t put anything in the result, either. It took me a long time to think of trying to make the original script by “store script.”

Thanks for all guidance…–Jim