[PD] Random without repeating numbers

Mathieu Bouchard matju at sympatico.ca
Thu Nov 28 16:05:34 CET 2002


On Thu, 28 Nov 2002, Enrique Franco wrote:

> Does anyone knows how to get the random object working without
> repeating numbers? I mean, for instance, if the random range is 5 I
> want to get a different number each time.

If you want sequences of five different numbers from 0 to 4, then you can
start with the list "0 1 2 3 4", pick a random valid position in that list
and remove the number at that position. That gives you equal access to the
5*4*3*2*1=120 possibilities.

If instead you just want that two consecutive numbers never be the same;
then you always keep the previous number (P), and you use 4 as the range,
you pick a single random number (R), and you compute R+(R>=P). The trick
is that >= is an indicatrix, which means it's worth 1 if the condition is
true, and 0 if it's false; and adding that indicatrix to the number itself
will cause the previous number to be skipped as a possibility.

e.g. P=2, R's might be 0,1,2,3, then R>=P might be 0,0,1,1, then
R+(R>=P) might be 0,1,3,4, and there you go, 2 is skipped.

You will also need the [t] object to route R into >= before +.

________________________________________________________________
Mathieu Bouchard                       http://artengine.ca/matju





More information about the Pd-list mailing list