[PD] Random counter

Frank Barknecht fbar at footils.org
Tue May 11 12:17:17 CEST 2004

```Hallo,
timon botez hat gesagt: // timon botez wrote:

> Im trying to find an object that will do a random set number - eg. 20
> to 100 without repeating the same number twice. Any clues?

This is Mathieu's elegant solution from the archives at
http://iem.kug.ac.at/mailinglists/pd-list/2002-11/011117.html

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

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 +.

Attached is a realization of the second idea as an abstraction.

Ciao
--
Frank Barknecht                               _ ______footils.org__
-------------- next part --------------
#N canvas 153 94 837 655 10;
#X text 325 349 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
+.;
#X obj 52 76 inlet;
#X obj 95 531 outlet;
#X obj 136 73 inlet;
#X obj 129 198 random;
#X obj 179 140 f \\$1;
#X obj 179 171 - 1;
#X msg 247 99 bang;
#X obj 111 408 >=;
#X obj 127 339 r \\$0-prev;
#X obj 117 484 s \\$0-prev;
#X msg 52 115 bang;
#X text 135 44 set range;
#X text 50 47 bang;
#X obj 95 458 +;
#X obj 129 225 s \\$0-rand;
#X obj 37 340 r \\$0-rand;
#X text 131 246 create random number in range (0 \, max-1);
#X text 58 310 compute R + (R >= P);
#X obj 37 368 t f f;
#X text 108 505 remember previous number;
#X connect 1 0 12 0;
#X connect 3 0 5 0;
#X connect 4 0 16 0;
#X connect 5 0 6 0;
#X connect 6 0 4 1;
#X connect 7 0 5 0;
#X connect 8 0 5 0;
#X connect 9 0 15 1;
#X connect 10 0 9 1;
#X connect 12 0 4 0;
#X connect 15 0 11 0;
#X connect 15 0 2 0;
#X connect 17 0 20 0;
#X connect 20 0 15 0;
#X connect 20 1 9 0;
-------------- next part --------------
#N canvas 0 0 450 300 10;
#X obj 86 78 norepeat 2;
#X floatatom 86 118 5 0 0 0 - - -;
#X msg 86 40 bang;
#X obj 174 78 norepeat 5;
#X obj 226 171 ==;
#X floatatom 174 115 5 0 0 0 - - -;
#X obj 264 147 f;
#X obj 221 116 t f f b;
#X obj 227 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0
1;
#X obj 270 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 1
1;
#X obj 269 171 !=;
#X connect 0 0 1 0;
#X connect 2 0 0 0;
#X connect 2 0 3 0;
#X connect 3 0 5 0;
#X connect 3 0 7 0;
#X connect 4 0 8 0;
#X connect 6 0 4 1;
#X connect 6 0 10 1;
#X connect 7 0 4 0;
#X connect 7 0 10 0;
#X connect 7 1 6 1;
#X connect 7 2 6 0;
#X connect 10 0 9 0;
```