Generate a random number between 1 and 8 using Javascript

Torstein Strand's icon

Hi!

I'm not sure if this has already been answered already, but I couldn't find it anywhere. I am trying to incorporate a simple Javascript code in Max MSP to randomly generate a number between 1 and 8, which is then going to power a Preset object that generates random drum patterns. I know from Javascript that you can use the Math.random function, and that it is really easy. But in JS for Max it doesn't seem like that function is available.

What it the most efficient and fastest way to do this with JS in Max MSP?

Thanks!

ChrisD's icon

It is available see attached code for a very simple example. If you have trouble with Javascript in Max look at the examples provided in the documentation and spend a little time taking them apart and modifying them and in a few hours you should no longer have any trouble making your own scripts.

Chris

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

basic_rand_JS.js
js
ChrisD's icon

You could also do this with plain old max objects like random (don't forget to add 1) or expr using its random function or the CNMAT object random in range.

Chris

Torstein Strand's icon

Thanks for the reply!

Yes, I'm aware that I can use Random, but this is for a school project where my grade will be significantly higher if i incorporate Javascript as well.

I'll look at the code you attached, thanks.

And also: Is it possible to change the number of rows in the Preset object? I'm using only 8 presets, but I can't delete the two other rows.. I've tried the help function with no luck. Maybe there's another object that has this ability? It's for aesthetic purposes.

Thanks, Torstein

Screen-Shot-2017-01-08-at-12.37.44.png
png
Source Audio's icon

Just resize the preset to Your needs. Grab it at the right bottom corner and...