easier way to do this?

Feb 24, 2007 at 8:57pm

easier way to do this?

Hi all,

I created a small abstraction (actually recreated it, but that’s
another story…) that takes a float and returns another float within
+/- N% of the input float. It functions just fine, but the algorithm
seems a little overly torturous to me. I’m wondering what an easier/
simpler/more elegant way to accomplish this might be, using only
standard Max objects. Could this all, for example, be done within an
[expr]? Any suggestions would be educational and appreciative.

Thanks in advance,

Steven

max v2;
#N vpatcher 10 59 444 381;
#P window setfont “Sans Serif” 9.;
#P user com 21 264 109 196617 11;
#K set 0 28277 28002 25970 8303 30068 28789 29728 10342 27759 24948
10496;
#K end;
#P user com 191 26 111 196617 12;
#K set 0 29281 28263 25888 10277 10528 26990 28789 29728 10342 27759
24948 10496;
#K end;
#P window linecount 3;
#P user com 168 193 134 196617 29;
#K set 0 29281 28260 28525 27769 8292 25955 26980 25888 30568 25972
26725 29216 29807 8289 25700 8303 29216 29557 25204 29281 25460 8308
28463 26226 28525 8302 30061 25189 29184;
#K end;
#P window linecount 5;
#P user com 238 127 134 196617 63;
#K set 0 25448 28527 29541 8306 24942 25711 27936 26990 29797 26469
29216 30569 29800 26990 8306 24942 26469 8240 8308 28448 29539 24940
25956 8306 24942 26469 8297 28272 30068 11296 29800 25966 8307 25441
27749 8290 24931 27424 25711 30574 8308 28448 26220 28513 29728 24942
25632 30067 25888 29807 8295 25972 8304 25970 25445 28276 24935 25888
28518 8302 30061 25189 29184;
#K end;
#P window linecount 1;
#P newex 208 44 82 196617 patcherargs 1.;
#P newex 210 84 41 196617 * 100.;
#P newex 210 104 27 196617 + 1;
#P newex 177 146 61 196617 * 0.0001;
#P newex 177 127 43 196617 random;
#P newex 21 226 27 196617 + 0.;
#P newex 139 205 29 196617 * 1.;
#P newex 35 184 125 196617 if $i1 == 0 then -1 else 1;
#P newex 21 106 53 196617 t f b f b;
#P newex 35 165 38 196617 decide;
#P newex 191 65 29 196617 t b f;
#P newex 158 165 29 196617 * 1.;
#P newex 21 87 27 196617 f;
#N comlet (float) output = input within +/-% range;
#P outlet 21 245 15 0;
#N comlet (float/int) percentage offset range;
#P inlet 191 44 15 0;
#N comlet (float/int) input number;
#P inlet 21 44 15 0;
#P window linecount 3;
#P user com 251 84 134 196617 35;
#K set 0 29539 24940 25888 29281 28263 25888 26990 28789 29728 26220
28513 29728 30064 8308 28448 26990 29797 26469 29216 26223 29216
26990 28789 29728 29807 8306 24942 25711 27948 8308 26725 28192 24932
25632 12544;
#K end;
#P window linecount 1;
#P comment 290 44 100 196617 default to +/- 1%;
#P user com 21 26 100 196617 10;
#K set 0 28277 28002 25970 8297 28272 30068 8232 26220 28513 29737;
#K end;
#P connect 3 0 6 0;
#P fasten 8 0 6 0 196 85 26 85;
#P connect 6 0 10 0;
#P connect 10 0 13 0;
#P connect 13 0 5 0;
#P connect 10 1 9 0;
#P connect 9 0 11 0;
#P fasten 12 0 13 1 144 224 43 224;
#P fasten 11 0 12 0 40 203 144 203;
#P fasten 10 2 7 0 54 144 163 144;
#P connect 7 0 12 1;
#P fasten 10 3 14 0 68 125 182 125;
#P connect 14 0 15 0;
#P connect 15 0 7 1;
#P connect 4 0 8 0;
#P fasten 18 0 8 0 213 63 196 63;
#P connect 8 1 17 0;
#P connect 17 0 16 0;
#P fasten 16 0 14 1 215 125 215 125;
#P pop;

—-
Steven M. Miller

Home < http://pubweb.csf.edu/~smill>
SFIFEM <
http://sfifem.csf.edu>
Atrium Sound Space <
http://atrium.csf.edu>
OVOS <
http://pubweb.csf.edu/~smill/ovos.html>

#30448
Feb 24, 2007 at 11:00pm

I find it really meditative to work on things like this! But i struggled a little to improve on your patch until i realised that by doubling the random amount and subtracting half you can eliminate the need for the decide and if section.

Because of this it’s about 30% faster on my machine.

My patch is included below….

take care,

john.

#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P comment 230 40 56 196617 test speed;
#P button 33 167 15 0;
#P button 199 168 15 0;
#P button 213 38 15 0;
#P newex 213 60 27 196617 b;
#P flonum 230 157 59 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#N vpatcher 525 182 715 430;
#N comlet interval in ms is reported here;
#P outlet 66 173 15 0;
#P window setfont “Sans Serif” 9.;
#P newex 96 82 13 196617 b;
#P newex 96 104 46 196617 cpuclock;
#P newex 66 143 27 196617 – 0.;
#P newex 48 82 13 196617 b;
#P newex 48 104 46 196617 cpuclock;
#N comlet bang stops timing and reports interval;
#P inlet 96 58 15 0;
#N comlet bang starts timing interval;
#P inlet 48 58 15 0;
#P connect 0 0 3 0;
#P connect 3 0 2 0;
#P connect 5 0 4 0;
#P connect 4 0 7 0;
#P connect 2 0 4 1;
#P connect 1 0 6 0;
#P connect 6 0 5 0;
#P pop;
#P newobj 230 134 55 196617 p cputimer;
#P newex 213 86 135 196617 uzi 10000;
#P button 48 39 15 0;
#P newex 48 61 27 196617 b;
#P flonum 65 149 59 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#N vpatcher 525 182 715 430;
#N comlet interval in ms is reported here;
#P outlet 66 173 15 0;
#P window setfont “Sans Serif” 9.;
#P newex 96 82 13 196617 b;
#P newex 96 104 46 196617 cpuclock;
#P newex 66 143 27 196617 – 0.;
#P newex 48 82 13 196617 b;
#P newex 48 104 46 196617 cpuclock;
#N comlet bang stops timing and reports interval;
#P inlet 96 58 15 0;
#N comlet bang starts timing interval;
#P inlet 48 58 15 0;
#P connect 0 0 3 0;
#P connect 3 0 2 0;
#P connect 5 0 4 0;
#P connect 4 0 7 0;
#P connect 2 0 4 1;
#P connect 1 0 6 0;
#P connect 6 0 5 0;
#P pop;
#P newobj 65 126 55 196617 p cputimer;
#P newex 48 87 135 196617 uzi 10000;
#P flonum 222 276 104 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 281 192 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 222 192 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#N vpatcher 20 74 1010 531;
#P origin 0 6;
#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P newex 538 250 27 196617 – 0;
#P newex 521 184 27 196617 t i i;
#P newex 519 208 27 196617 * 2;
#P user com 332 324 109 196617 11;
#K set 0 28277 28002 25970 8303 30068 28789 29728 10342 27759 24948 10496;
#K end;
#P user com 502 86 111 196617 12;
#K set 0 29281 28263 25888 10277 10528 26990 28789 29728 10342 27759 24948 10496;
#K end;
#P newex 519 104 82 196617 patcherargs 1.;
#P newex 521 144 41 196617 * 100.;
#P newex 521 164 27 196617 + 1;
#P newex 485 272 61 196617 * 0.0001;
#P newex 485 229 43 196617 random;
#P newex 332 286 27 196617 + 0.;
#P newex 332 166 53 196617 t f b f b;
#P newex 502 125 29 196617 t b f;
#P newex 466 293 29 196617 * 1.;
#P newex 332 147 27 196617 f;
#N comlet (float) output = input within +/-% range;
#P outlet 332 305 15 0;
#N comlet (float/int) percentage offset range;
#P inlet 502 104 15 0;
#N comlet (float/int) input number;
#P inlet 332 104 15 0;
#P comment 601 104 100 196617 default to +/- 1%;
#P user com 332 86 100 196617 10;
#K set 0 28277 28002 25970 8297 28272 30068 8232 26220 28513 29737;
#K end;
#P fasten 7 0 5 0 507 145 337 145;
#P connect 2 0 5 0;
#P connect 5 0 8 0;
#P connect 8 0 9 0;
#P connect 9 0 4 0;
#P fasten 6 0 9 1 471 315 367 315 367 282 354 282;
#P fasten 8 2 6 0 365 204 471 204;
#P fasten 8 3 10 0 379 185 490 185;
#P connect 19 0 11 0;
#P connect 11 0 6 1;
#P fasten 14 0 7 0 524 123 507 123;
#P connect 3 0 7 0;
#P connect 17 0 10 1;
#P connect 18 0 17 0;
#P connect 7 1 13 0;
#P connect 13 0 12 0;
#P connect 12 0 18 0;
#P connect 10 0 19 0;
#P fasten 18 1 19 1 543 205 560 205;
#P pop;
#P newobj 222 234 69 196617 p my version;
#P flonum 66 272 104 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 132 188 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 66 189 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#N vpatcher 20 74 620 474;
#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P user com 68 266 109 196617 11;
#K set 0 28277 28002 25970 8303 30068 28789 29728 10342 27759 24948 10496;
#K end;
#P user com 238 28 111 196617 12;
#K set 0 29281 28263 25888 10277 10528 26990 28789 29728 10342 27759 24948 10496;
#K end;
#P window linecount 3;
#P user com 215 195 134 196617 29;
#K set 0 29281 28260 28525 27769 8292 25955 26980 25888 30568 25972 26725 29216 29807 8289 25700 8303 29216 29557 25204 29281 25460 8308 28463 26226 28525 8302 30061 25189 29184;
#K end;
#P window linecount 5;
#P user com 285 129 134 196617 63;
#K set 0 25448 28527 29541 8306 24942 25711 27936 26990 29797 26469 29216 30569 29800 26990 8306 24942 26469 8240 8308 28448 29539 24940 25956 8306 24942 26469 8297 28272 30068 11296 29800 25966 8307 25441 27749 8290 24931 27424 25711 30574 8308 28448 26220 28513 29728 24942 25632 30067 25888 29807 8295 25972 8304 25970 25445 28276 24935 25888 28518 8302 30061 25189 29184;
#K end;
#P window linecount 1;
#P newex 255 46 82 196617 patcherargs 1.;
#P newex 257 86 41 196617 * 100.;
#P newex 257 106 27 196617 + 1;
#P newex 224 148 61 196617 * 0.0001;
#P newex 224 129 43 196617 random;
#P newex 68 228 27 196617 + 0.;
#P newex 186 207 29 196617 * 1.;
#P newex 82 186 125 196617 if $i1 == 0 then -1 else 1;
#P newex 68 108 53 196617 t f b f b;
#P newex 82 167 38 196617 decide;
#P newex 238 67 29 196617 t b f;
#P newex 205 167 29 196617 * 1.;
#P newex 68 89 27 196617 f;
#N comlet (float) output = input within +/-% range;
#P outlet 68 247 15 0;
#N comlet (float/int) percentage offset range;
#P inlet 238 46 15 0;
#N comlet (float/int) input number;
#P inlet 68 46 15 0;
#P window linecount 3;
#P user com 298 86 134 196617 35;
#K set 0 29539 24940 25888 29281 28263 25888 26990 28789 29728 26220 28513 29728 30064 8308 28448 26990 29797 26469 29216 26223 29216 26990 28789 29728 29807 8306 24942 25711 27948 8308 26725 28192 24932 25632 12544;
#K end;
#P window linecount 1;
#P comment 337 46 100 196617 default to +/- 1%;
#P user com 68 28 100 196617 10;
#K set 0 28277 28002 25970 8297 28272 30068 8232 26220 28513 29737;
#K end;
#P window linecount 0;
#P comment 140 158 100 196617;
#P connect 4 0 7 0;
#P fasten 9 0 7 0 243 87 73 87;
#P connect 7 0 11 0;
#P connect 11 0 14 0;
#P connect 14 0 6 0;
#P connect 11 1 10 0;
#P connect 10 0 12 0;
#P fasten 13 0 14 1 191 226 90 226;
#P fasten 12 0 13 0 87 205 191 205;
#P fasten 11 2 8 0 101 146 210 146;
#P connect 8 0 13 1;
#P fasten 11 3 15 0 115 127 229 127;
#P connect 15 0 16 0;
#P connect 16 0 8 1;
#P connect 5 0 9 0;
#P fasten 19 0 9 0 260 65 243 65;
#P connect 9 1 18 0;
#P connect 18 0 17 0;
#P fasten 17 0 15 1 262 127 262 127;
#P pop;
#P newobj 66 230 76 196617 p your version;
#P comment 65 41 56 196617 test speed;
#P connect 5 0 8 0;
#P connect 7 0 5 1;
#P connect 6 0 5 0;
#P connect 20 0 2 0;
#P connect 19 0 6 0;
#P fasten 14 2 6 0 342 182 227 182;
#P connect 14 1 15 1;
#P connect 15 0 16 0;
#P connect 17 1 15 0;
#P connect 17 0 14 0;
#P connect 18 0 17 0;
#P fasten 9 2 2 0 177 175 71 175;
#P connect 13 0 12 0;
#P connect 12 0 9 0;
#P connect 12 1 10 0;
#P connect 10 0 11 0;
#P connect 9 1 10 1;
#P connect 1 0 4 0;
#P connect 3 0 1 1;
#P connect 2 0 1 0;
#P window clipboard copycount 22;

#97397
Feb 24, 2007 at 11:13pm

… and one with even less objects using ‘scale’ it’s slightly less fast than my previous example though….

#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P comment 237 47 56 196617 test speed;
#P button 206 175 15 0;
#P button 220 45 15 0;
#P newex 220 67 27 196617 b;
#P flonum 237 164 59 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#N vpatcher 525 182 715 430;
#N comlet interval in ms is reported here;
#P outlet 66 173 15 0;
#P window setfont “Sans Serif” 9.;
#P newex 96 82 13 196617 b;
#P newex 96 104 46 196617 cpuclock;
#P newex 66 143 27 196617 – 0.;
#P newex 48 82 13 196617 b;
#P newex 48 104 46 196617 cpuclock;
#N comlet bang stops timing and reports interval;
#P inlet 96 58 15 0;
#N comlet bang starts timing interval;
#P inlet 48 58 15 0;
#P connect 0 0 3 0;
#P connect 3 0 2 0;
#P connect 5 0 4 0;
#P connect 4 0 7 0;
#P connect 2 0 4 1;
#P connect 1 0 6 0;
#P connect 6 0 5 0;
#P pop;
#P newobj 237 141 55 196617 p cputimer;
#P newex 220 93 135 196617 uzi 10000;
#P flonum 229 283 104 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 288 199 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 229 199 50 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#N vpatcher 20 74 1010 531;
#P origin 0 6;
#P window setfont “Sans Serif” 9.;
#P newex 526 111 82 196617 patcherargs 1.;
#P newex 557 204 34 196617 * -1.;
#P newex 490 263 132 196617 scale 0 999 -1.1.;
#P user com 339 331 109 196617 11;
#K set 0 28277 28002 25970 8303 30068 28789 29728 10342 27759 24948 10496;
#K end;
#P user com 509 93 111 196617 12;
#K set 0 29281 28263 25888 10277 10528 26990 28789 29728 10342 27759 24948 10496;
#K end;
#P newex 528 151 41 196617 * 100.;
#P newex 585 178 27 196617 t f f;
#P newex 507 292 61 196617 * 0.0001;
#P newex 481 222 70 196617 random 1000;
#P newex 339 293 27 196617 + 0.;
#P newex 339 173 53 196617 t f b f b;
#P newex 509 132 29 196617 t b f;
#P newex 461 296 29 196617 * 1.;
#P newex 339 154 27 196617 f;
#N comlet (float) output = input within +/-% range;
#P outlet 339 312 15 0;
#N comlet (float/int) percentage offset range;
#P inlet 509 111 15 0;
#N comlet (float/int) input number;
#P inlet 339 111 15 0;
#P user com 339 93 100 196617 10;
#K set 0 28277 28002 25970 8297 28272 30068 8232 26220 28513 29737;
#K end;
#P fasten 6 0 4 0 514 152 344 152;
#P connect 1 0 4 0;
#P connect 4 0 7 0;
#P connect 7 0 8 0;
#P connect 8 0 3 0;
#P fasten 5 0 8 1 466 322 374 322 374 289 361 289;
#P fasten 7 2 5 0 372 211 466 211;
#P connect 10 0 5 1;
#P fasten 7 3 9 0 386 192 486 192;
#P connect 9 0 15 0;
#P connect 15 0 10 0;
#P connect 17 0 6 0;
#P connect 2 0 6 0;
#P connect 6 1 12 0;
#P connect 11 0 16 0;
#P connect 16 0 15 3;
#P connect 12 0 11 0;
#P connect 11 1 15 4;
#P pop;
#P newobj 229 241 78 196617 p my version 2;
#P connect 2 0 0 1;
#P connect 0 0 3 0;
#P connect 1 0 0 0;
#P fasten 4 2 1 0 349 191 234 191;
#P connect 8 0 7 0;
#P connect 7 0 4 0;
#P connect 7 1 5 0;
#P connect 5 0 6 0;
#P connect 4 1 5 1;
#P connect 9 0 1 0;
#P window clipboard copycount 11;

#97398
Feb 24, 2007 at 11:23pm

Of course! Thanks!

It’s funny…I was just telling one of my students about that very
technique a few days ago when discussing scaling one range of numbers
to another…drew him an example patch and everything…then promptly
forgot all about it when making my abstraction today.

The speed gain is about the same on my machine (2.16 GHz MacBook Pro,
2GB RAM, MaxMSP 4.6.2) as what you report.

Thanks again,

Steven

On Feb 24, 2007, at 11:00 PM, Leafcutter John wrote:

>
> I find it really meditative to work on things like this! But i
> struggled a little to improve on your patch until i realised that
> by doubling the random amount and subtracting half you can
> eliminate the need for the decide and if section.
>
> Because of this it’s about 30% faster on my machine.
>
> My patch is included below….
>

—-
Steven M. Miller

Home < http://pubweb.csf.edu/~smill>
SFIFEM <
http://sfifem.csf.edu>
Atrium Sound Space <
http://atrium.csf.edu>
OVOS <
http://pubweb.csf.edu/~smill/ovos.html>

#97399
Feb 24, 2007 at 11:24pm

Yeah, the speed gain of this one is minimal on my machine, as well.

Thanks again!

On Feb 24, 2007, at 11:13 PM, Leafcutter John wrote:

>
> … and one with even less objects using ‘scale’ it’s slightly less
> fast than my previous example though….
>

—-
Steven M. Miller

Home < http://pubweb.csf.edu/~smill>
SFIFEM <
http://sfifem.csf.edu>
Atrium Sound Space <
http://atrium.csf.edu>
OVOS <
http://pubweb.csf.edu/~smill/ovos.html>

#97400
Feb 26, 2007 at 1:15am

i think your gut reaction that your patch was too complicated was accurate. i found a way to do it using 8 standard objects. see below:

-scott

max v2;
#N vpatcher 10 59 926 664;
#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P newex 350 266 27 196617 + 1.;
#P flonum 333 327 58 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 272 123 27 196617 t f b;
#P newex 333 295 27 196617 * 1.;
#P newex 379 150 27 196617 * 2.;
#P newex 350 240 33 196617 * 0.01;
#P newex 350 213 27 196617 – 1.;
#P newex 350 124 69 196617 t b f f;
#P newex 350 177 39 196617 random;
#P flonum 350 101 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 272 101 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P comment 266 83 100 196617 number;
#P comment 350 83 100 196617 range (%);
#P comment 424 181 193 196617 generate random number from 0 to range*2;
#P window linecount 2;
#P comment 421 210 207 196617 subtract range from random number , resulting in a random number from -range to range;
#P window linecount 1;
#P comment 421 244 100 196617 divide by 100;
#P comment 420 269 100 196617 add 1;
#P comment 421 297 100 196617 multiply by number;
#P window linecount 3;
#P comment 253 359 319 196617 basically , the algorithm is to multiply the original number by a random number from (1-range/100) to (1+range/100). for instance , if range=50% , then you would multiply the number by a random number from 0.5 to 1.5;
#P connect 8 0 16 0;
#P fasten 16 0 15 0 277 256 338 256;
#P connect 15 0 17 0;
#P connect 9 0 11 0;
#P fasten 16 1 10 0 294 162 355 162;
#P connect 11 0 10 0;
#P connect 10 0 12 0;
#P connect 12 0 13 0;
#P connect 13 0 18 0;
#P connect 18 0 15 1;
#P fasten 11 2 12 1 413 203 372 203;
#P connect 11 1 14 0;
#P connect 14 0 10 1;

#97401
Feb 26, 2007 at 1:31am

Thanks Scott – I knew I was making it overly complicated…it’s funny
how you get caught up in things and just can’t see straight.

Best,

Steven

On Feb 25, 2007, at 6:15 PM, Scott wrote:

>
> i think your gut reaction that your patch was too complicated was
> accurate. i found a way to do it using 8 standard objects. see
> below:
>
> -scott
>

—-
Steven M. Miller

Home < http://pubweb.csf.edu/~smill>
SFIFEM <
http://sfifem.csf.edu>
Atrium Sound Space <
http://atrium.csf.edu>
OVOS <
http://pubweb.csf.edu/~smill/ovos.html>

#97402
Feb 26, 2007 at 4:57am

well, LCJohn made a fine point, that, it’s very worthwhile to meditate on these sorts of things. That is to say, if you want to be the best Maxer this side of the Mississippi.

#97403

You must be logged in to reply to this topic.