Re: Random Numbers fasters than the osax
Re: Random Numbers fasters than the osax
- Subject: Re: Random Numbers fasters than the osax
- From: Ed Stockly <email@hidden>
- Date: Sat, 30 Mar 2002 13:55:02 -0800
Here's a thought...
If you're concerned that generating a random number may take too long at
execution time, try this:
set listORandoms to {}
repeat 5000 times
set the end of listORandoms to random number
end repeat
You could easily insert this snippet early in your script, say at
launch, and then just grab the items from the listORandoms variable as
they are needed without going to the Osax when time is critical.
Here's an example you can paste into your script editor:
set timeList to {}
repeat 10 times
set startTime to the current date
GetNextRandom()
set endTime to current date
set elapsedTime to endTime - startTime
set the end of timeList to elapsedTime
end repeat
return timeList
on GetNextRandom()
global listORandoms
global randomIndex
try
set randomIndex to randomIndex + 1
on error
set randomIndex to 1
end try
try
set newRandom to item randomIndex of listORandoms
on error
set listORandoms to {}
repeat 5000 times
set the end of listORandoms to random number
end repeat
set newRandom to item randomIndex of listORandoms
end try
return newRandom
end GetNextRandom
On my imac when I use this handler the first time I call it takes 1
second, each time after that takes a fraction of a second (I don't have
ticks on OS X or I'd be more precise).
You may want to save a few ticks and some memory by generating fewer
random numbers to start with. If it ever runs out of them, it will
simply build a new list.
HTH,
ES
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.