Prevent the same random number from being output twice in a row.

Dominic Muir's icon

Hi there,

I am selecting a random 'buffer~' from a 'polybuffer~' object. To do this, I insert the dumpout value of the polybuffer~ into the right inlet of a' random' object to set the range. The output of the 'random' object allows me to then 'play~' the corresponding buffer using a 'set' message.

How can I prevent the same buffer from playing back twice in a row, or in simple terms, how can I prevent the same random value from being output twice in a row. I thought of using an 'if' statement to compare the last random number with the new one but I am not sure that this is the most efficient method.

Thanks in advanced.

LSka's icon

depending on your desired output, you can use either [urn] or [zl change] (see patch below):

Max Patch
Copy patch and select New From Clipboard in Max.

Dominic Muir's icon

Thanks very much!

zl change is just what I was looking for.

Christopher Dobrian's icon

The 'if' method is fine, provided you have a minimum of 2 possible random choices (ensure that the range of the 'random' or 'urn' object is at least 2). The urn object produces random choices that never repeat, but when you reset it there's of course the possibility that the first new choice could be the same as the last choice prior to the reset, so if you absolutely want no repeats, you still need to test the output and choose again if there's a repeat.

Max Patch
Copy patch and select New From Clipboard in Max.

BTW, a little nerdy detail, with the method shown on the left, using the random object, each number always has an equal likelihood of being chosen each time except for the previous number which has no likelihood of being chosen, whereas with the urn object shown on the right each number that has been chosen since the last reset has a zero likelihood of being chosen, and the remaining unchosen numbers naturally have an increased likelihood of being chosen.

Dominic Muir's icon

Thanks for the tip Christopher, I have actually implemented a 'zl change' object as suggested earlier by LSKA.

I still have a problem though - my random generator is inside a poly~ object. This means that if I generate 2 random values, the second instance of the poly doesn'trefer to the first poly's 'zl' object. This means that there is still a chance for repetition.

Is there any way around this?

Dominic Muir's icon

Should I use the second input of the 'zl change' somehow?

Christopher Dobrian's icon

If you will have multiple random selection objects that need to be aware of each other's most recent output, you'll need to store the most recently chosen output (from any and all of them) in a global variable using 'send' and 'receive', or 'value'. Here's an example.

Max Patch
Copy patch and select New From Clipboard in Max.

Roman Thilenius's icon

if $i1==$i2 then out2 bang else out $i1

where $i2 is $i1 (t-1)

Dominic Muir's icon

Thanks Christopher.

This was what I was trying to originally but I wasn't aware of the 'value' object to set a global variable.

Thanks for your advice everyone!