Negative random number

neilhu's icon

How do I create a negative random number?

I am looking to create a random integer number between -1 and 1, so the values would be -1, 0 and 1.
I am having trouble figuring out how to do this.
Any help would be great!

Thanks
Neil

Mike S's icon
Max Patch
Copy patch and select New From Clipboard in Max.
neilhu's icon

That's a very neat solution, thank you!

Roman Thilenius's icon

[expr (random(0\,2))-1]

Stephane Morisse's icon

Mike S, your solution for float can't reach one (or it's too early in the morning for me...).

Stephane Morisse's icon

Mike S, your solution for float can't reach 1.0 (or it's too early in the morning for me...).
edit : hiccup, double post sorry

Christopher Dobrian's icon
Max Patch
Copy patch and select New From Clipboard in Max.
Beep's icon

here you go:

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

woyteg's icon

@ barry, is this more efficient than subtraction? ie. mike s' solution for integer?

Beep's icon

No idea I'm afraid...

Mike S's icon

Hi Stephan,

Change the random object to 201, then it will reach 1.

Peter Castine's icon

@Woyteg: In general, fewer objects means less overhead.

Also: subtraction is (at least in principle) always more efficient than branching ("if" logic), and [gate] presumably is based on "if" logic. However, when patching in Max, the overhead for message-passing is several orders of magnitude higher than the difference between subtraction and branching at the machine level. So this is something you really don't need to lose sleep over.

What bothers me more about Mike's (float) solution is the granularity of the output. This may not matter in a lot of applications, but if your set up is such that different things happen for 0.0, 0.005, and 0.01; well, 0.005 isn't going to happen in this patch. You can work around that, of course, following Chris' example of higher granularity. You simply have to know what you want and how to get it.

For my part, I'd use lp.shhh and take the 24-bit granularity and run with it. At this point in time with Max 6 (and unfortunately), that is only an option for people using Litter Power Pro. Working on this, though.

Roman Thilenius's icon

i dont think 5 objects are more effective than 1 or 2.
but i still like his solution. it had to be added here for the sake of completeness, and hopefully some expansion of conciousness.

and where you need only 2 values there is the good old [chance] object, which could trigger two sexy [zl reg] to store and trigger anything you like.

-110

Yujia YANG's icon

It's useful this page, thanks everyone!