my orderListByRandom function

In case this helps anyone else, I had a few scenarios where I really want things to happen in a random order (a good QA strategy, rather than always being the same).

There doesn't seem to be a built-in way, so here's what I came up with. Just pass a comma delimited list and you'll get it returned in a different order.

scriptname : orderListByRandom
params vList
/// should be a comma separated list .

set vListCount to the number of items in vList 
set rOrderList to any item of vList & "," // need to start the new random Order list so we can validate against it.  

repeat while the number of items in rOrderList is less than vListCount
	
	set newItem to any item of vList
	set uniqueItem to "" // reset it (always a good idea when we're looping)
	
	repeat with each item in rOrderList
		if newItem = it 
			set uniqueItem to false
			exit repeat // we don't need further evaluation since it matched. 
		else
			set uniqueitem to true
		end if
	end repeat 
	
	if uniqueItem = true
		put newItem & "," after rOrderList
		//put "Items in random list: " && the number of items in rOrderList  //debug
	end if
	
end repeat

if the last character in rOrderList is "," // trim it.
	delete the last character in rOrderList
end if

//put rOrderList // debug

return rOrderList

use this to see that it does order a list at random:
put (1,2,3,4,5) into mylist

repeat 5 times
	put orderListByRandom(mylist)
end repeat

Your output will list 5 different orders of the items (numbers) passed. Hope this helps someone.

Comments

  • SenseTalkDougSenseTalkDoug ForumAdmin admin
    Hi, I hate to rain on your parade after you've obviously put a lot of good effort into developing this code. There is an easier way, although it's not very obvious, and is clearly something that we should document somewhere.

    The easy solution is to use the sort command and random numbers:
    put (1,2,3,4,5) into myList
    
    sort myList by random(1000)
    
    put myList
    
    I've thought about adding a shuffle or randomize function to the language to make this more explicit and easier to find, but for now just use sort. And if there's something you need to do that isn't obvious, feel free to ask here!
  • brett_xbrett_x Member
    Thanks Doug.

    I figured one of you might point out a built-in way to do it. It wasn't too much effort as I have done something similar (comparing 2 lists) in the past. I was more worried you might point out a fatal flaw (like passing an extra comma - which occurred to me, but I haven't tried it as I plan on controlling the lists).

    I did search the Sensetalk browser for Sort, and it came up with Zero. I also did find the documentation that mentioned sorting, but yes, it's light.

    Of course, anytime a post like this goes up, it helps someone if they search (I did). So this is a form of documentation.
  • EggplantMattEggplantMatt ForumAdmin admin
    I'd also point out that you started out defining an actual list object [(1,2,3,4,5)], but then you ended up treating it as a string. There shouldn't be any need to manage the commas, because they're not part of the actual list -- they're just the visual delimiters of the items. I'd forgotten Doug's shortcut and was going to offer this alternative:
    put a..z as list into alphas -- Create a range of letters
    
    put 1..5 as list into nums -- Create a range of numbers
    
    
    repeat 20 times // generate a list of random 5 character strings
    	insert (any item of alphas for each item of nums) joined by empty after Names 
    end repeat
    
    put names
    
    repeat the number of items in Names
    	put random(the number of items in Names) into itemNo
    	insert item  itemNo of Names after randomNames
    	delete item itemNo of Names
    end repeat
    
    put randomNames
    
Sign In or Register to comment.