HISE Logo Forum
    • Categories
    • Register
    • Login

    random float 0. - 1. how to?

    Scheduled Pinned Locked Moved Scripting
    6 Posts 2 Posters 47 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      Rognvald
      last edited by

      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);
      
      iamlampreyI 1 Reply Last reply Reply Quote 0
      • iamlampreyI
        iamlamprey @Rognvald
        last edited by

        @Rognvald

        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.

        R 1 Reply Last reply Reply Quote 0
        • R
          Rognvald @iamlamprey
          last edited by

          @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);
              }
          };
          
          iamlampreyI 1 Reply Last reply Reply Quote 0
          • iamlampreyI
            iamlamprey @Rognvald
            last edited by

            @Rognvald You don't pass anything into Math.random():

            Math.random(0.0, 1.0); // wrong
            Math.random(); // right
            
            R 1 Reply Last reply Reply Quote 0
            • R
              Rognvald @iamlamprey
              last edited by

              @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);
              
              iamlampreyI 1 Reply Last reply Reply Quote 0
              • iamlampreyI
                iamlamprey @Rognvald
                last edited by

                @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 the setAttribute function twice.

                1 Reply Last reply Reply Quote 2
                • First post
                  Last post

                25

                Online

                2.0k

                Users

                12.8k

                Topics

                111.0k

                Posts