How to use a generated number once, before going on to the next number?

edited June 2017 in How To...

I have managed to generate random numbers between 0 and 100, however, the program keeps on going with this function. It doesn't just pick one random number as needed, it just keeps on going with a list of different numbers. How do I change this?

(Sorry, noob with code here)

Tagged:

Answers

  • Answer ✓

    Are you running your random number generator inside your draw() function? That is what it seems it is happening here although it is a guess since you didn't provide your code. In general, draw runs 60 fps. You want to avoid your definition in that function. Instead, use a different approach like the one outlined below.

    Kf

    final float LOWLIM=0;
    final float HIGHLIM=100;
    
    float myNum;
    
    void setup(){ size(400,600);}
    
    void draw(){
      text("Press the space bar to get a number...",width/4,height/2);
    }
    
    void keyPressed(){
       if(keyCode==' '){
          myNum=random(LOWLIM,HIGHLIM);
          println("My current random value is: " + myNum);
       }
    }
    
  • Answer ✓

    Store in a variable

  • I have it in a void display function

  • how do you store in a variable? the value will still keep on constantly changing

  • Answer ✓

    So you only want to pick one random number? Run random() once in setup(), and store the result in a variable.

    int result;
    
    void setup(){
      size(400,400);
      result = int(random(100));
    }
    
    // ...
    
  • ok I gave it more thought, i think i know what i need to do now

Sign In or Register to comment.