### Negative random number

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

<code>

**all**of the following text. Then, in Max, select

*New From Clipboard*.

```
----------begin_max5_patcher----------
548.3ocyVssabBCD8Y3qvxOVQP9BKP5a86HpphKNItBrWAFksMJ+601.6RR6
tf2EQievVdXr8YNblw9UeOXt7.qEB9J3Afm2q9ddVSFCdCy8f0YGJpxZstAK
j00LgBFz+ME6fxZ+wJY1Qq6aXsZmxTbo3GMrBU+IPwnPT.HN1ziF5.eebQYp
hm4hmlr.bxNiWDRpYfRsSltFQWMWTwTVrgGLxKsHRl+y6vIiP5QoPIxpY1O8
sFdVE7zdH6TiaBZh+s7ea8GSBQFqu46a5BtQphKT.34C5jz9v7d6Ph6wb7+w
XVvdQig+JjuCfQnv4jGjHSvhiSBo5FIcghjd8DNhDFqaIj.PD9xrF4evZQtx
ZiTeuI0u1y5wyPpvwSdEXU8NpOa3rb.QSxibvNj6JGpqb.4xbP..lmIdZU4h
ynv9hQlfmmhvI3SxDh6xD7mXYxYnllLQor1HNV.8foGS9FJ95D8fVM5QWkbU
Im7NkRJtTcWa0mzioMNl7D6X7MjXbM+l6pyYMyFI3j6C2cU0AhVsx.lehaXQ
.80LySKQwFZg5dtO4Sq19xI9z44jIY8wWwcmaNwX8AVwEe7wq1PzX+8rUqrq
oX7PFxVAmBvRVqhKruGYhOlhYSb5YdYISL88R07x8RMjGvPJ0HrvnH6EL6hC
rue8clNS99RQ8GPzYfcjav9VwDdIXhtsXJZIXBuoXZQJtMEQjEfnMVKs.DQt
ADom7l+e.PoxqYD
-----------end_max5_patcher-----------
```

</code>

That’s a very neat solution, thank you!

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

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

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

<code>

**all**of the following text. Then, in Max, select

*New From Clipboard*.

```
----------begin_max5_patcher----------
434.3ocwTE0aBBCD9Y3WQSeZtnFJJJr21uikkkBT0ZfVRo3by3+8UtBSbgEI
NiqOT3tq8tuuubWO35fik6YkXzSnWPNNGbcb.W0NbZrcv4z8IYzR3XXYkNio
wisgDU4bgwFhQZbVP0Ia3h0uoXIZatm4O0aLh3GV+wOncG85o7XSLjHuFu7T
aIi2NgDzVxDYdNS.4E+vpLIUOBonhTYNxjkXlBsRY9eBAokHBt9RGccq2FOP
JJXuaJYa8zr81h8HxapW8hzK68uH68lW+YQDvduek8sxn0k9iBlMKXfr3uuU
G0IpEQqjBsflC2.+rhSy5Foj+IDgXvysSWlXXFrtNYIzCTkfqUU3h90jv+SM
oogz+uzuDrv10Pt0Jyx6hx.LsWl6cIl6OePOS7qDuWV2+CHwTw5Qcej.tJNi
K94yh.Zq8eNmKkUpj1B2TGzIDmxJ0bAUykhNmY4YmYCOMkI5pLo7RZbFC.e+
Z+PQyxAflv6FZhF.ZHA2M3DN.3Dcknw1GQKJ1wTkMoD.hYJYqTUatXLXxEVS
ncFqX63smeF3gpLSIZyHRkxNotObgoi0TmiteEPQG6C
-----------end_max5_patcher-----------
```

</code>

here you go:

**all**of the following text. Then, in Max, select

*New From Clipboard*.

```
----------begin_max5_patcher----------
586.3ocyW10aaBCEF9Z3WAx2NZE1DHI6t86XZpxD7RcEXiLltrU0+6ybLjjV
EwWi41KR.eve7lGrOum7huGJSdhUiB9Zv2C77dw2yCB0FvqqsGpjd5PAsF5F
Rv9kL6ITn8QZ1IMD9KA39X+TJz07+vZiiI2G0EV1nKXZ8uqX1kCwElg9itmJ
ZJ4ByygEgbIncTPTbWzJp9vibwwGTrCZ6bsIxrLA3svkXx8IgADy8mmcdNHR
ivuCGesNEzRPNnuo3zBT6Cd02u8qv+MdTHo4YTww4BEXL2lJ34RERRKNH.TR
i5u+lPIZkgRSYFSgNKMkYF0L0CLAMq.l6nErYIbA3gL1lFxd3Rxf7YuS1ynW
yyPyd2xtTf.cTYyP3XqivQzGHNRimLNRcDNt6ib6QOHrW1MDORbBONR0rf34
Rj1jHvm437DOJVhrIYSFBKabz1jrfrE45rxoWstO6rVxCd9wMNxFGHkzjmOZ
EsjIKyRNYB6VHNAJJpHWVN+iQqesackofGrLkowDXnnBt38U1Bqda72BpZYi
5P+ur9ZDCtnfbVslKnZtTbUm17l97HOOmIttxlbdcawNf5it4arIKmInl2I4
+qxIZB5g3L4rcBpYuyTS5mJ0j7oRMsmXHinlsNUM3QTSpSUyXuoRblZlZ9O7
.pojmWIM9Dc4fMk.X9W43DHA+9jyMtrJqp5G6MK1YrjLAVtzj2VmNZU0yLUc
2TBBwX9+jT01LMDZxE1lfQLRwdl22en3VDUY7k0FS4Fk0U8ztTimpYcd0+u.
ViQHBC
-----------end_max5_patcher-----------
```

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

No idea I’m afraid…

Hi Stephan,

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

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

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

Forums > MaxMSP