Generate a random list or array using Javascript

Torstein Strand's icon

Hey,

I'm trying to use Javascript to create a random list that consists of four numbers: 1,2,3 and 4. I javascript to create a random pattern i.e. 3,2,4,1 so that it will send the message to Matrixctrl to create a random drum pattern. I've looked online and found out that I can sort an array in a random order using this code:

var points = [1, 2, 3, 4,];
points.sort(function(a, b){return 0.5 - Math.random()});

However, I don't know how to incorporate this into Javascript for Max, because I have to make it so that Javascript sends out a random list from outlet 0.

How would I write the code in order for Max to read it properly? This is my best best so far:

inlets = 1;
outlets = 1;

setinletassist(0, "bang");
// so that Javascript knows when to start using a bang object in Max
var points = [1, 2, 3, 4,];
// The start of my array, which I want Javascript to spew out randomly

function myFunction() {
points.sort(function(a, b){return 0.5 - Math.random()});
document.getElementById("demo").innerHTML = points;
}
function bang(){
outlet(0, myFunction)
}

Thanks in advance!

Torstein

Screen-Shot-2017-01-08-at-21.12.11.png
png
Screen-Shot-2017-01-08-at-21.12.07.png
png
Christopher Dobrian's icon

You don't need JavaScript for this. Here are two ways to do it in Max.

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

Torstein Strand's icon

Thanks for the reply,

I am aware that I can do it in Max, but I want to incorporate Javascript because this is a school project and my grades will be significantly better if I use Javascript as well. Should've mentioned that in the topic above...

Do you - or anyone else - know of any way to do this with Javascript in Max?

Thanks

Nikolas K's icon

Hi Torstein,

The JS you posted is in the right way, and will work with some changes.
- Delete the "document.getElementById("demo").innerHTML = points;"
- Set the "myFuntion" to return the points array.
- And add the () at the outlet(0, myFunction());
If you do not ad the parenthesis, it will post the "name" of the function (which is an object), so you will get something like jsobject 8237468952346.

I have attached a zip with a patcher and the js that works and just has the changes i mentioned above.
Hope it helps.

-N

JsRandSort.zip
zip
Torstein Strand's icon

Hey! Thanks a lot for the reply, it works.

The only thing is that it doesn't really generate completely random, but the coordinates 321 every third time i bang JS. Do you know of any way to make it more random? And also, Matrixctrl says that the coordinates are out of range sometimes, and it doesn't really affect the pattern. I think there must be something wrong with the coordinates I'm generating using JS. Do you know a good way to generate random matrixctrl patterns using JS? It would be much appreciated! As i said above, this is for my final exam in Audio Programming and it will really help my grade and a good way to learn more about JS in Max.

Thanks, Torstein

José López-Montes's icon

Hi,

I don't understand why this linepoints.sort(function(a, b) { return 0.5 - Math.random(); });
uses the arguments a and b. This seems to work fine for me:points.sort(function() { return 0.5 - Math.random(); });
jlm

José López-Montes's icon

I just found this interesting article which strongly recommends avoiding the array.sort method approach to get good randomized results:Array.sort() should not be used to shuffle an array