Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    Creating a "random" number in a macro

    Scheduled Pinned Locked Moved Solved
    Gcode meta commands
    3
    9
    615
    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.
    • Exerqtorundefined
      Exerqtor
      last edited by Exerqtor

      I'm trying to make a macro for purging and cleaning the nozzle, with the purge bucket having two containers (one on each side of the brush).
      And to keep only one of the purge buckets filling up i want to generate a variable to select what bucket to use randomly for each time.

      My goal is to "randomly" output "0" or "1", with 0/1 being the left/right bucket (0=left & 1=right). But i'm having problems creating the conditional GCode to output 0 or 1 at random.
      Could someone please help me along? 🤣

      I'll post the macro once i've got the randomizing working here so those who feel like it can take a look and give feedback (it's based on a klipper macro).

      EDIT: Using RRF 3.4.1 by the way!

      fcwiltundefined HebigTundefined 2 Replies Last reply Reply Quote 0
      • fcwiltundefined
        fcwilt @Exerqtor
        last edited by

        @exerqtor said in Creating a "random" number in a macro:

        I'm trying to make a macro for purging and cleaning the nozzle, with the purge bucket having two containers (one on each side of the brush).
        And to keep only one of the purge buckets filling up i want to generate a variable to select what bucket to use randomly for each time.

        My plan goal is to "randomly" output "0" or "1", with 0/1 being the left/right bucket (0=left & 1=right). But i'm having problems creating the conditional GCode to output 0 or 1 at random.
        Could someone please help me along? 🤣

        I'll post the macro once i've got the randomizing working here so those who feel like it can take a look and give feedback (it's based on a klipper script).

        Well you don't say what firmware you are using but under 3.x there is a random function the will generate numbers.

        Frederick

        Printers: a E3D MS/TC setup and a RatRig Hybrid. Using Duet 3 hardware running 3.4.6

        Exerqtorundefined 1 Reply Last reply Reply Quote 0
        • HebigTundefined
          HebigT @Exerqtor
          last edited by

          @exerqtor @fcwilt Does RRF support the modulus operator? If so, maybe you could generate a random number using the 'random' function fcwilt mentioned and then create a new variable = 0 or 1 if the random number is odd or even (using modulus %)

          1 Reply Last reply Reply Quote 0
          • Exerqtorundefined
            Exerqtor @fcwilt
            last edited by

            @fcwilt

            Yeah sorry i forgot to mention that, i'm on 3.4.1, and i've seen/found the function in the docs but i don't understand how to use / impliment it since i can't find any examples of it being used (not a programmer lol).

            using this as an example for what i want

            var bucket_pos = 0
            
            set var.bucket_pos = {random function to output 0 or 1}
            fcwiltundefined 2 Replies Last reply Reply Quote 0
            • fcwiltundefined
              fcwilt @Exerqtor
              last edited by

              @exerqtor said in Creating a "random" number in a macro:

              @fcwilt

              Yeah sorry i forgot to mention that, i'm on 3.4.1, and i've seen/found the function in the docs but i don't understand how to use / impliment it since i can't find any examples of it being used (not a programmer lol).

              using this as an example for what i want

              var bucket_pos = 0
              
              set var.bucket_pos = {random function to output 0 or 1}
              

              Hold on - let me see what the syntax is - only take a minute or two.

              Frederick

              Printers: a E3D MS/TC setup and a RatRig Hybrid. Using Duet 3 hardware running 3.4.6

              1 Reply Last reply Reply Quote 0
              • fcwiltundefined
                fcwilt @Exerqtor
                last edited by fcwilt

                @exerqtor said in Creating a "random" number in a macro:

                @fcwilt

                Yeah sorry i forgot to mention that, i'm on 3.4.1, and i've seen/found the function in the docs but i don't understand how to use / impliment it since i can't find any examples of it being used (not a programmer lol).

                using this as an example for what i want

                var bucket_pos = 0
                
                set var.bucket_pos = {random function to output 0 or 1}
                

                set var.random_number = random(10) will set the variable to a pseudo-random integer number from 0 to 9.

                So random(2) should return either 0 or 1.

                Frederick

                Printers: a E3D MS/TC setup and a RatRig Hybrid. Using Duet 3 hardware running 3.4.6

                Exerqtorundefined 1 Reply Last reply Reply Quote 2
                • Exerqtorundefined
                  Exerqtor @fcwilt
                  last edited by

                  @fcwilt

                  Ok so if i get it right, this:

                  var bucket_pos = 0
                  
                  set var.bucket_pos = random(2)
                  

                  Will end up making the "bucket_pos" variable either 0 or 1?

                  fcwiltundefined 1 Reply Last reply Reply Quote 0
                  • fcwiltundefined
                    fcwilt @Exerqtor
                    last edited by

                    @exerqtor said in Creating a "random" number in a macro:

                    @fcwilt

                    Ok so if i get it right, this:

                    var bucket_pos = 0
                    
                    set var.bucket_pos = random(2)
                    

                    Will end up making the "bucket_pos" variable either 0 or 1?

                    That syntax is correct and the docs say random returns a number from 0 to one less than the operand.

                    So if the docs are correct you will get a variable with 0 or 1.

                    Frederick

                    Printers: a E3D MS/TC setup and a RatRig Hybrid. Using Duet 3 hardware running 3.4.6

                    Exerqtorundefined 1 Reply Last reply Reply Quote 0
                    • Exerqtorundefined
                      Exerqtor @fcwilt
                      last edited by

                      @fcwilt said in Creating a "random" number in a macro:

                      @exerqtor said in Creating a "random" number in a macro:

                      @fcwilt

                      Ok so if i get it right, this:

                      var bucket_pos = 0
                      
                      set var.bucket_pos = random(2)
                      

                      Will end up making the "bucket_pos" variable either 0 or 1?

                      That syntax is correct and the docs say random returns a number from 0 to one less than the operand.

                      So if the docs are correct you will get a variable with 0 or 1.

                      Frederick

                      Well in that case i'll make a new thread for the macro in question and refer to this thread in a few. Thx! ❤

                      1 Reply Last reply Reply Quote 1
                      • Exerqtorundefined Exerqtor marked this topic as a question
                      • Exerqtorundefined Exerqtor has marked this topic as solved
                      • Exerqtorundefined Exerqtor referenced this topic
                      • Exerqtorundefined Exerqtor referenced this topic
                      • Exerqtorundefined Exerqtor referenced this topic
                      • Exerqtorundefined Exerqtor referenced this topic
                      • Exerqtorundefined Exerqtor referenced this topic
                      • First post
                        Last post
                      Unless otherwise noted, all forum content is licensed under CC-BY-SA