How do I set random values in processing that are in certain intervals





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















For example, I'm trying to make it so when one of the circles goes off the edge of the screen, it resets it's X-coordinates to anywhere from -100,0 or 840,940. This is because I want it to move back onto the screen from another random location.



Is there a way to use the random function in processing like this?



The only way I could think of to get around this would be doing this:



//in my real program this would be taking place in a for loop parsing through an array 
int = random(1,2);
if(x = 1){
posX[i] = random(-100,0);
}
else{
posX[i] = random(840,940);
}


But this would take extra space. I assume there is some way to just create random values within a certain interval.










share|improve this question































    0















    For example, I'm trying to make it so when one of the circles goes off the edge of the screen, it resets it's X-coordinates to anywhere from -100,0 or 840,940. This is because I want it to move back onto the screen from another random location.



    Is there a way to use the random function in processing like this?



    The only way I could think of to get around this would be doing this:



    //in my real program this would be taking place in a for loop parsing through an array 
    int = random(1,2);
    if(x = 1){
    posX[i] = random(-100,0);
    }
    else{
    posX[i] = random(840,940);
    }


    But this would take extra space. I assume there is some way to just create random values within a certain interval.










    share|improve this question



























      0












      0








      0








      For example, I'm trying to make it so when one of the circles goes off the edge of the screen, it resets it's X-coordinates to anywhere from -100,0 or 840,940. This is because I want it to move back onto the screen from another random location.



      Is there a way to use the random function in processing like this?



      The only way I could think of to get around this would be doing this:



      //in my real program this would be taking place in a for loop parsing through an array 
      int = random(1,2);
      if(x = 1){
      posX[i] = random(-100,0);
      }
      else{
      posX[i] = random(840,940);
      }


      But this would take extra space. I assume there is some way to just create random values within a certain interval.










      share|improve this question
















      For example, I'm trying to make it so when one of the circles goes off the edge of the screen, it resets it's X-coordinates to anywhere from -100,0 or 840,940. This is because I want it to move back onto the screen from another random location.



      Is there a way to use the random function in processing like this?



      The only way I could think of to get around this would be doing this:



      //in my real program this would be taking place in a for loop parsing through an array 
      int = random(1,2);
      if(x = 1){
      posX[i] = random(-100,0);
      }
      else{
      posX[i] = random(840,940);
      }


      But this would take extra space. I assume there is some way to just create random values within a certain interval.







      processing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 6:58









      Kevin Workman

      34.4k54273




      34.4k54273










      asked Nov 22 '18 at 4:41









      user10487132user10487132

      113




      113
























          1 Answer
          1






          active

          oldest

          votes


















          0














          What you have is basically the approach I would take. There are a few issues with your code: you're using = instead of ==, and you're comparing exactly 1 instead of a range. You probably want something like this instead:



          float x = random(1);
          if(x < .5){
          myValue = random(-100, 0);
          }
          else{
          myValue = random(840, 940);
          }


          One cool thing about programming is that you can make your own functions, like this:



          float getValueFromRanges(float startOne, float endOne, float startTwo, float endTwo){

          if(random(1) < .5){
          return random(startOne, endOne);
          }
          else{
          return random(startTwo, endTwo);
          }
          }


          Then you could just call this function whenever you wanted a random value from those ranges:



          float myValue = getValueFromRanges(-100, 0, 840, 940);


          Or you could do this in one line of code using the ternary operator:



          float myValue = random(1) < .5 ? random(-100, 0) : random(840, 940);


          This last approach is not very readable, so I'd probably go with creating a function.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423999%2fhow-do-i-set-random-values-in-processing-that-are-in-certain-intervals%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            What you have is basically the approach I would take. There are a few issues with your code: you're using = instead of ==, and you're comparing exactly 1 instead of a range. You probably want something like this instead:



            float x = random(1);
            if(x < .5){
            myValue = random(-100, 0);
            }
            else{
            myValue = random(840, 940);
            }


            One cool thing about programming is that you can make your own functions, like this:



            float getValueFromRanges(float startOne, float endOne, float startTwo, float endTwo){

            if(random(1) < .5){
            return random(startOne, endOne);
            }
            else{
            return random(startTwo, endTwo);
            }
            }


            Then you could just call this function whenever you wanted a random value from those ranges:



            float myValue = getValueFromRanges(-100, 0, 840, 940);


            Or you could do this in one line of code using the ternary operator:



            float myValue = random(1) < .5 ? random(-100, 0) : random(840, 940);


            This last approach is not very readable, so I'd probably go with creating a function.






            share|improve this answer




























              0














              What you have is basically the approach I would take. There are a few issues with your code: you're using = instead of ==, and you're comparing exactly 1 instead of a range. You probably want something like this instead:



              float x = random(1);
              if(x < .5){
              myValue = random(-100, 0);
              }
              else{
              myValue = random(840, 940);
              }


              One cool thing about programming is that you can make your own functions, like this:



              float getValueFromRanges(float startOne, float endOne, float startTwo, float endTwo){

              if(random(1) < .5){
              return random(startOne, endOne);
              }
              else{
              return random(startTwo, endTwo);
              }
              }


              Then you could just call this function whenever you wanted a random value from those ranges:



              float myValue = getValueFromRanges(-100, 0, 840, 940);


              Or you could do this in one line of code using the ternary operator:



              float myValue = random(1) < .5 ? random(-100, 0) : random(840, 940);


              This last approach is not very readable, so I'd probably go with creating a function.






              share|improve this answer


























                0












                0








                0







                What you have is basically the approach I would take. There are a few issues with your code: you're using = instead of ==, and you're comparing exactly 1 instead of a range. You probably want something like this instead:



                float x = random(1);
                if(x < .5){
                myValue = random(-100, 0);
                }
                else{
                myValue = random(840, 940);
                }


                One cool thing about programming is that you can make your own functions, like this:



                float getValueFromRanges(float startOne, float endOne, float startTwo, float endTwo){

                if(random(1) < .5){
                return random(startOne, endOne);
                }
                else{
                return random(startTwo, endTwo);
                }
                }


                Then you could just call this function whenever you wanted a random value from those ranges:



                float myValue = getValueFromRanges(-100, 0, 840, 940);


                Or you could do this in one line of code using the ternary operator:



                float myValue = random(1) < .5 ? random(-100, 0) : random(840, 940);


                This last approach is not very readable, so I'd probably go with creating a function.






                share|improve this answer













                What you have is basically the approach I would take. There are a few issues with your code: you're using = instead of ==, and you're comparing exactly 1 instead of a range. You probably want something like this instead:



                float x = random(1);
                if(x < .5){
                myValue = random(-100, 0);
                }
                else{
                myValue = random(840, 940);
                }


                One cool thing about programming is that you can make your own functions, like this:



                float getValueFromRanges(float startOne, float endOne, float startTwo, float endTwo){

                if(random(1) < .5){
                return random(startOne, endOne);
                }
                else{
                return random(startTwo, endTwo);
                }
                }


                Then you could just call this function whenever you wanted a random value from those ranges:



                float myValue = getValueFromRanges(-100, 0, 840, 940);


                Or you could do this in one line of code using the ternary operator:



                float myValue = random(1) < .5 ? random(-100, 0) : random(840, 940);


                This last approach is not very readable, so I'd probably go with creating a function.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 6:58









                Kevin WorkmanKevin Workman

                34.4k54273




                34.4k54273
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423999%2fhow-do-i-set-random-values-in-processing-that-are-in-certain-intervals%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)