Random lists

Hi,

Reading in another post about creating a random list, I ended up with this:

-- get 1000 random numbers from 1 to 1000

set list_length to 1000

-- create the list of indices
set index_list to {}
repeat with i from 1 to list_length
	set s to space & (text -4 thru -1 of ("000" & i)) & space
	set end of index_list to s
end repeat
set index_string to index_list as string
set s_length to 6

set num_items to 1000

-- create the list of random numbers
set low_num to list_length - num_items + 1
set rand_list to {}
repeat with rand_max from list_length to low_num by -1
	-- get a string item
	set r to random number from 1 to rand_max with seed 0
	set i to (s_length * (r - 1)) + 1
	set s_item to text i thru (i + s_length - 1) of index_string
	set end of rand_list to (s_item as integer)
	-- remove the string item
	set tids to AppleScript's text item delimiters
	set AppleScript's text item delimiters to s_item
	set temp_list to text items of index_string
	set AppleScript's text item delimiters to {""}
	set index_string to temp_list as string
	set AppleScript's text item delimiters to tids
end repeat
{count rand_list, rand_list contains 1, rand_list contains 1000, rand_list}

Does anyone see something that is slowing it down?

Thanks,
kel

To whom it may concern,

That’s alright. On the first few runs it was a bit slow. A few minutes later, I tried it again and it sped up.

Thanks anyway,
kel

Text, unreferenced list indexing, and ˜random number’. :slight_smile:

-- get 1000 random numbers from 1 to 1000

set list_length to 1000

-- create the list of indices
set index_list to {}
repeat with i from 1 to list_length
	set end of my index_list to i
end repeat

set rand_list to {}
repeat list_length times
	set n to some integer of my index_list
	set end of my rand_list to n
	set item n of my index_list to missing value
end repeat
{count rand_list, rand_list contains 1, rand_list contains 1000, rand_list}

Hi Nigel,

Good puzzle! I love puzzles, so don’t tell me the answer.

Thanks,
kel

ps. yet

Think I’ve got one. There’s a better way to write ‘text m thru n’. Need to think on that.

Hi Nigel,

I didn’t see that you had rewritten the script. That’s great! :cool: How the hell did you do that so fast.

Thanks,
kel

I hate to act like Columbo, but one more thing. What if you want to get only part of the list. That’s why I put the:

set num_items to 1000

part in the script. I still don’t understand why my script has the lag at the beginning.

Edied: I didn’t mean at the beginning, I meant on the first run.

Oh I see now what you’re doing. Integers of the list. Nice one! :cool::cool:

Thanks a lot,
kel