Random switching - not sure how :-(

Discuss problems and solutions.
Post Reply
jimmyz
Newbie
Posts: 5
Joined: 28 Apr 2015 04:54

Random switching - not sure how :-(

Post by jimmyz »

Hi, I'm new here, but been using Lemur on the iPad since it was released, but only programming simple stuff... though I'd like to get more advanced.

Hope someone can help me - I've searched as much as I can and can't find an answer. I want to use a pad to randomly select an active cell from a multi-segmented switch. Sorry if I my question doesn't make sense. So for example, I have a 1x4 row switch, if I tap on a pad I want the active cell in the row to change, randomly.

Can this be achieved easily?
Softcore
Regular
Posts: 1613
Joined: 04 Nov 2012 08:34

Re: Random switching - not sure how :-(

Post by Softcore »

Lets suppose your switch object is named "Switches". What we need is a way to get a random value to turn ONE of the switches on, correct? Well then we need random values in the range of amount of switches we have minus 1.
Why? Picture this.....we have a switch with 4 rows. Then each of the swtiches is actually....

Switches.x[0], Switches.x[1], Switches.x[2], Switches.x[3]

That is, our switches can be accesed by using Switches.x[n] where n is the amount of switches minus 1. OR in lemur scripts that is sizeof(Switches.x) - 1.

We create a script inside the Pad object....
we set it to execute on expression x, up arrow (whenever the Pad is pressed)
One first step is for sure, we will be using the rand() function which returns a random floating value (a random number between zero and 1). But we dont want a random value in the range of 0 to 1 - we want a random value in the range of, as seen above, sizeof(Switches.x)-1.

So...in the first line of our script we declare a variable and generate our random value.....

Code: Select all

decl r = rand()*(sizeof(Switches.x)-1);
Mentally perform the action above, take a random value in the range of 0 to 1, for example 0.875 and multiply it with sizeof(Switches.x)-1. You 'll get
0.875 * 3 = 2.625.

Right? Right....

next step is to make sure we turn OFF the switches - because obviously, at the time we pressed the Pad to randomize them, surely one switch might already be active.

So next line:

Code: Select all

Switches.x = 0;
Now all we have to is round our generated random value (2.625) to the closest integer and assign the corresponding switch to ON.

So next line:

Code: Select all

Switches.x[round(r)]=1;
(for our example Switches.x[round(2.625)]=1; -> Switches.x[3]=1 -> the last switch will be on)

Go ahead and mentally put any sort of value for r - you will see that it will always randomely switch on ONE of the switches because our random value is in the range of 0 to 3 and we always round it to an integer.

To recap, here's the final script that SHOULD reside inside the Pad object:

Code: Select all

decl r=rand()*(sizeof(Switches.x)-1);
Switches.x=0;
Switches.x[round(r)]=1;
of course, if you know you wont be changing the amount of switches in the future (1x4 rows) it CAN be typed:

Code: Select all

decl r=rand()*3;
Switches.x=0;
Switches.x[round(r)]=1;
Softcore
Regular
Posts: 1613
Joined: 04 Nov 2012 08:34

Re: Random switching - not sure how :-(

Post by Softcore »

Going the extra mile......

It is going to happen, once in a few times, that the generated random value returns the SAME switch to be on, with the one currently being on. In simple words, once in a while, tapping on our Pad object will seemignly have no effect on the switches (but what it does actually is, switching the switches off, then switch on the SAME switch that was previously on).

If this function is undesired, we can insert a "while" loop in our script that will check if the generated random value corresponds to the switch which is currently on, and if so....re-generate a new value.

So our above script can be typed as:

Code: Select all

decl r=rand()*(sizeof(Switches.x)-1);
while (Switches.x[round(r)]==1){
r=rand()*(sizeof(Switches.x)-1);
}
Switches.x=0;
Switches.x[round(r)]=1;
jimmyz
Newbie
Posts: 5
Joined: 28 Apr 2015 04:54

Re: Random switching - not sure how :-(

Post by jimmyz »

Maaaate - sensational! This did just the trick... and you did it so quickly too, very much appreciated! :ugeek:
jimmyz
Newbie
Posts: 5
Joined: 28 Apr 2015 04:54

Re: Random switching - not sure how :-(

Post by jimmyz »

Just thinking about this, I wonder if it would be simple to use a range object for the randomization action to reference, to restrict the randomised results.

That is, say I have a user configurable, segmented range slider and it could be adjusted so only cells 3 to 7 are selected, and then pressing the pad will randomise only within that range in the row of associated switches, and return a result within that range only.

The overall idea here is to produce an "intelligent" patch randomizer for a synth. I'm sure you know what I mean; some randomized results I want to exclude each time I punch the "randomize" pad. I need to do this for faders as well, but I can probably work that out myself, it's these switches that have me stumped.

Cheers!
Softcore
Regular
Posts: 1613
Joined: 04 Nov 2012 08:34

Re: Random switching - not sure how :-(

Post by Softcore »

Ah yes, I love those "guided randomizations" too. In fact, about the faders - knobs I have already done such stuff, and in fact you can already see such an example in the "files" of the Lemur Users FB group (check my signature) in the form of a complete tutorial. Im planning to make that tutorial into a .pdf file and offer it in user library together with the resulting template.

Now, as for the switches....yeah still doable. Lets say we have a 1x8 Switches object then shall we?

According to what we did previously we will also have a Pad object with this script (set to execute on x, up arrow) in it:

Code: Select all

decl r=rand()*7;
Switches.x=0;
Switches.x[round(r)]=1;
We will now insert a "Range" object and put it next to our Switches object and set its grid setting to 9. This will make the "steps" of the Range object visually represent each button. Using the Range object we can, for the time visually, exclude upper or lower switches.

Now, it is sure we will use the range(x,min, max) function (math internal functions of Lemur) but since we are not mathematicians and we are a bit lost we insert two Monitor objects in our template to observe the Range.x[0] and Range.x[1] values. We do that by using these values in the Monitor objects.

A first thought is to use Range.x[0]*7 as minimum value and Range.x[1]*7 as a maximum value - the goal is to retrieve a 0 rising to 7 for our minimumj value and a 7 falling to 0 for our maximum value. Toying around with the range object and observing the monitors we can quickly see thats NOT the case.
Instead what we are after is
minimum = 7-Range.x[0]*7
maximum= 7-Range.x[1]*7

With this in mind, we dont even have to go "circles" - remember that Range objects x[0] and x[1] are always in the range of 0 to 1 - as our initial rand() function is.
So instead of calculating a rand()*7 and THEN confine it in a range of minimum to maximum as noted above, we do the range thing BEFORE multiplying with 7.

SO......

In our final script with the range incorporated we have:

Code: Select all

decl r=range(rand(),1-Range.x[0],1-Range.x[1])*7;
Switches.x=0;
Switches.x[round(r)]=1;
Try it, works as expected!

Perosnal note: IF I had to make such a "guided randomization" thing for switches I wouldnt use a Range object - what if we want to exclude only two switches in the middle?
Instead I would use a secondary group of switches which would serve as "randomization enable - disable" for EACH of my initial switches.
Softcore
Regular
Posts: 1613
Joined: 04 Nov 2012 08:34

Re: Random switching - not sure how :-(

Post by Softcore »

And of course, the 'extra mile' we noted above will be:

Code: Select all

decl r=range(rand(),1-Range.x[0],1-Range.x[1])*7;
while (Switches.x[round(r)]==1){
r=range(rand(),1-Range.x[0],1-Range.x[1])*7;
}
Switches.x=0;
Switches.x[round(r)]=1;
Softcore
Regular
Posts: 1613
Joined: 04 Nov 2012 08:34

Re: Random switching - not sure how :-(

Post by Softcore »

Also, using light -2 on the "enable-disable randomization" switches and using LEDs above them, makes for a nicer, simpler and more elegant interface.

Here's all the above in an example template
Attachments
randomSwitches-RangedVSSwitched.jzml
(11.85 KiB) Downloaded 97 times
jimmyz
Newbie
Posts: 5
Joined: 28 Apr 2015 04:54

Re: Random switching - not sure how :-(

Post by jimmyz »

Softcore, I can't even begin to tell you how much I appreciate this - and I'm just astounded that you've helped me so thoroughly; that you gave this your time and consideration is just amazing - thank you!

I had a lot of success with the coding you suggested for the Range slider, but as you said, another Switch would be ideal - and that was my original intention, but I never thought to ask because I assumed it too difficult. I've implemented the "LED" version of your coding, but applied it to a set of Switches. The version you submitted in the thread I couldn't get to work, not sure why. I can see of course that both your Range and LED implementations in the attachment work just fine, and when applied in my project they work as well - but like I said, I used the LED coding for a Switch. I do like how elegantly the LEDs version looks, but for my project it will make the page too "busy" - I'll have the randomization range settings on a separate page (err... container); for all intents and purposes these will likely be set and forget, but the user will have a chance to tweak them if needed.

Thank you too for pointing me in the way of your other tutorials - I'll be sure to check them out as well.

Peace!
Softcore
Regular
Posts: 1613
Joined: 04 Nov 2012 08:34

Re: Random switching - not sure how :-(

Post by Softcore »

Probalby some naming - referencing of objects is wrong in your scripts thats why they wont work. For example, if you use a second set of Switches, then surely those cant be named "Switches" again.....

Aaaanyways....No worries! ;)
Post Reply