For those who want a random routine that selects an element from a list of objects - for example:
My list of objects I wish to randomly select is..
a = ['apple','watermelon','cheese','milk','oranges']
All you want is a random order or selection like..
cheese, apple, milk, watermelon and oranges
Then reset the list so you can again select from the list in a different random sequence.
To do this I use my RandomGen() class. It has the following parameters:
getRNDPosition(theArray,resetList)
- theArray is any linear Array type which represents the group of elements you want to cycle through randomly until all is selected. This array is a list of zeros that represent each element in your list of objects. In this case we have 5 elements in our sample list above.
- reseList is a boolean type (TRUE or FALSE - 1 or 0) that wants to know if you want to reset the array once it has chosen all of the elements in the group which simply means you selected everything.
Lets start creating this randomGen() class
Step 1: Random Array
The first thing I create in my class is the ability to create a group of elements that will represent my selection of objects. So I do something like this...
on createRandomArray(theCnt)
theRnd = []
repeat with z = 1 to theCnttheRnd.append(0)
end repeat
return theRnd
end
b = createRandomArray(5)
returns: [0,0,0,0,0]
So now when I call this function I get back an array that looks like this. I have to specify the number of elements to select from. Pass this function the count of your array objects (a.count) to get the exact objects needed.
Step 2: Random Generator
Now lets create the main method or function for randomization
on getRNDPosition(theArray,resetList)
if voidP(resetList)then resetList = true
if resetList parameter is not sent, set default to FALSErndCells = []
new array position to select from
repeat with z = 1 to theArray.countif theArray[z] = 0 then rndCells.append(z)
end repeat
repeat loop and create an array with only cell positions that have NOT been selected
if rndCells.count = 0 then
if resetList then
resetArray(theArray)
return getRNDPosition(theArray,resetList)else
return void
end if
end if
if the array contains nothing - all is selected then here is where we can reset the array
or we can return void for array completed
we'll build this function nextpickCell = random(rndCells.count)
select a random cell position from the array of non selected objects
pickPosition = rndCells[pickCell]
define pickPosition as the position of the selected cell
theArray[pickPosition] = 1
mark the cell position chosen as picked by changing 0 to 1
return pickPosition
return the selected cell position
end
Step 3:Reset Array
Now lets create the function for reseting the random array. This will require us to reset all the cellpositions from 1 to 0. We do this like this..
on resetArray theArray
repeat with z = 1 to theArray.count
theArray[z] = 0
end repeat
loop through the array and reset all objects to 0
return theArray
return reset array
end
Step 4: Using the code
Now lets use the function.
Define your objects
myObjects = ['apple','watermelon','cheese','milk','oranges']
Define the random var you want to control.
myRandomArray = createRandomArray(myObjects.count)
Now let's randomize
myPick = getRNDPosition(myRandomArray,true)
returns: 4,2,5,1 or 3
Get your selected object it represents from your array with the following.
myObject = myObjects[myPick]
Everytime you do myPick = getRNDPosition(myRandomArray,true) you'll get a different position until all are selected. Then the array will reset and you start all over again.
No comments:
Post a Comment