random float 0. - 1. how to?
-
How to convert "randInt" to float?
This compiles but does not seem to work..inline function onButton1Control(component, value) { if (value) { local Knob1_value = Math.randInt(20, 20000); local Knob2_value = Math.randInt(0, 100 * 0.01); Knob1.setValue(Knob1_value); Knob2.setValue(Knob2_value); } }; Content.getComponent("Button1").setControlCallback(onButton1Control); -
You can use
Math.random()for a random float between 0.0 and 1.0.You also need to add
Knob1.changed(); Knob2.changed();Afterwards if you want the affected controls to actually change anything.
-
@iamlamprey Thanks for your speedy reply!
i get this error.. Too many arguments in API call Math.random(). Expected: 0
inline function onButton6Control(component, value) { if (value) { local Knob1_value = Math.randInt(20, 20000); local Knob3_value = Math.random(0.0, 1.0) Knob1.setValue(Knob1_value); Knob1.changed(); Knob3.setValue(Knob3_value); Knob3.changed(); SimpleGain1.setAttribute(SimpleGain1.Gain, Knob1_value); SimpleGain2.setAttribute(SimpleGain2.Gain, Knob3_value); } }; -
@Rognvald You don't pass anything into Math.random():
Math.random(0.0, 1.0); // wrong Math.random(); // right -
@iamlamprey yes I got it thanks again for your time :) Still learning and enjoying HISE.
This is working for me*
inline function onButton6Control(component, value) { if (value) { local Knob1_value = Math.randInt(20, 20000); local Knob3_value = Math.range(Math.random(), 0.0, 1.0); Knob1.setValue(Knob1_value); Knob1.changed(); Knob3.setValue(Knob3_value); Knob3.changed(); SimpleGain1.setAttribute(SimpleGain1.Gain, Knob1_value); SimpleGain2.setAttribute(SimpleGain2.Gain, Knob3_value); } }; Content.getComponent("Button6").setControlCallback(onButton6Control); -
@Rognvald No problem

This is a bit cleaner:
inline function onButton6Control(component, value) { if (!value) { return; } // works the same as if (value) but the scope is a little bit cleaner local Knob1_value = Math.randInt(20, 20000); // this is already fine local Knob3_value = Math.random(); // you don't need range here, Math.random() is already within the range you're expecting Knob1.setValue(Knob1_value); Knob1.changed(); // these can be single lines if you prefer Knob3.setValue(Knob3_value); Knob3.changed(); }; Content.getComponent("Button6").setControlCallback(onButton6Control);If Knob1 and Knob3 already have their own Control Callbacks, you also don't need to independently call these:
SimpleGain1.setAttribute(SimpleGain1.Gain, Knob1_value); // already handled by Knob1.changed(); SimpleGain2.setAttribute(SimpleGain2.Gain, Knob3_value); // already handled by Knob3.changed();Control.changed()is essentially simulating changing the control with the mouse, so you're basically calling thesetAttributefunction twice.