7 Replies Latest reply on Jul 12, 2011 12:13 PM by Production Monkey

# Delete from a random list a number equal to another

Hello,

I need to create a list of random numbers.
The list of random numbers should not contain the same number, if it generated they must be deleted.
The total number contained in the list is a variable defined in another script.

If my variable is 90, for example, how can I do to continue to generate random numbers until I reach 90 and at the same time delete a number equal to another?

Thanks.

• ###### 1. Re: Delete from a random list a number equal to another

You just need to use "getPos" to see if a number is already in the list before adding it.

on getRandomList HowMany, MinRndNum, MaxRndNum
-- setup default random number range
if MinRndNum.voidP then MinRndNum = 1
if MaxRndNum.voidP then MaxRndNum = 1000 + HowMany  -- May want to change these default values

-- make sure "HowMany" is passed in and won't create an infinite loop
if HowMany.voidP then return []
if  HowMany > MaxRndNum then
put "getRandomList() error.  List count can not exceed MaxRndNum."
return []
end if

-- create list of random numbers
RndNumList = []
repeat while RndNumList.count <= HowMany
NextRndNum = random(MinRndNum, MaxRndNum)
if RndNumList.getPos(NextRndNum) = 0 then  -- this number is not in the list. Add it.
end if
end repeat

return RndNumList
end getRandomList

• ###### 2. Re: Delete from a random list a number equal to another

Here's a much simpler method than the one posted above:

```on shuffleList _list
_list = _list.duplicate()
rndList = []
cnt = _list.count
repeat with i = 1 to cnt
_list.deleteAt(rndNum)
end repeat
return rndList
end```

If you want a handler that will shuffle both a linear or property list then you can use this slightly expanded one below:

```on shuffleList _list
if not listP(_list) then return VOID
_list = _list.duplicate()
if ilk(_list) = #proplist then
isPropList = TRUE
rndList = [:]
else
isPropList = FALSE
rndList = []
end if
cnt = _list.count
repeat with i = 1 to cnt
rndNum = random(_list.count)
if isPropList then
else
end if
_list.deleteAt(rndNum)
end repeat
return rndList
end```
• ###### 3. Re: Delete from a random list a number equal to another

Thank you both for your kind responses.
I would like to follow the suggestion of jchunick because it seems easier. I'm beginner of director and that is why I am asking the following question.
I applied the script:

On shuffleList _list
_list _list.duplicate = ()
rndList = []
cnt = _list.count
repeat with i = 1 to cnt
_list.deleteAt (rndNum)
end repeat
end

.. to a movie script, and I called the "shuffleList" behavior from a button.
I assumed that "_list" is "my list" so I attributed its value and I also attributed to "rndNum" the value of the number of components of "my list".
When calling "shuffleList" I get: "Index out of range".
I guess I have committed many errors ...
Some further suggestions?

• ###### 4. Re: Delete from a random list a number equal to another

The only error you committed is using Josh's code.

I wrote a function that does all the work for you. You do not have to understand how it works. Just use it.

It is actually very simple. It just looks complicated because I made it idiot proof.

I guess you need an example. Let's say you want a list of 100 random numbers from 50 to 150 with each number in the list being unique. Just call the function.

From the message window you can test the function out thusly:  put getRandomList(100, 50, 150)

What is there to understand: You want 100 random numbers in a list from 50 to 150 and the function returns that.

Just try the code.

• ###### 5. Re: Delete from a random list a number equal to another

The only error you committed is using Josh's code.

Ooooh, snap!  We're gonna have us a Lingo rumble now.

• ###### 6. Re: Delete from a random list a number equal to another

Mike Blaustein wrote:

The only error you committed is using Josh's code.

Ooooh, snap!  We're gonna have us a Lingo rumble now.

lmao!

Well, I can see why my script won't work based off the OP's requirements... so, here's the same script, but modified to work as requested:

on shuffleList _maxVal

_list = []

repeat with i = 1 to _maxVal

_list.append(i)

end repeat

rndList = []

cnt = _list.count

repeat with i = 1 to cnt

rndNum = random(_list.count)

_list.deleteAt(rndNum)

end repeat

return rndList

end

-- where _maxVal is the " variable defined in another script." as the OP put it... in his example it'd be 90.

To explain, all we're doing is creating an ordered list with numbers from 1 to 90. To randomize it we simply generate a random number from the item count of the list, then add that to our randomized list (rndList) and lastly, delete that item from the original list (_list)... think of it as being no different than when they drop the lottery balls, in sequential order (our ordered list), into the lottery machine and then they are drawn out randomly from the lottery machine (our random list)... just extend that to drawing every single ball out instead of the 6 or 7 they draw during the lottery.

*** NOTE: I did miss the line, "rndNum = random(_list.count)", in the original post. That was certainly my mistake when I removed code from the more complex code that randomizes property lists too.

*** NOTE2: It should be pointed out that my original shuffleList handler is a better general purpose handler (with the forgotten line of code added) since you can generate whatever list you need before calling it, eg.:

someList = []

repeat with i = 1 to 90

someList.append(i)

end repeat

-- then call the original shuffleList handler

randomizedList = shuffleList(someList)