I'm having an issue with creating a randomised array in setup to then be printed out in draw

edited February 2014 in Questions about Code

Good afternoon all.

I'm new to Processing and this forum.

We've just started Processing in one of my University modules, we were set three challenges the number one and two were fine but the third I've been racking my brain over for a number of days now, I'm sure its pretty simple but I'm cleary missing it, could anybody help out on here for me. The challenge was as follows:

//challenge 3 - create a fixed array of 20 numbers and use a while loop to set them to random numbers in the setup method use a while loop like the one above to print them out to the screen.

Here is the code which I've used so far:

int[] randomNumbers = new int[20];


void setup(){

  size(640,480);
  frameRate(5);

  int counter = 0;

  while(counter < 20 ){ 

  int randomNumbers = (int)random(2,500);
    counter = counter + 1;
  }

}

void draw(){



   background(0);
   fill(255);
   stroke(255);

   int counter = 0;
    // this is a local variable - it only exists within the containing curly brackets

   while(counter < 20){ //this is a while loop
      //the code between these curly brackets 
      //will keep repeating until counter > 20

        int yPos = 20 + counter * 20; //look another local variable - it only exists inside the while loop

        text("counter = " + randomNumbers,20, yPos );

       counter = counter + 1; //increment the counter variable
     }

}

Cheers for any help

Gragnit

Answers

  • edited February 2014 Answer ✓

    you need to use the variable 'counter' that you have created in setup() as the index of the array to access it. for example:

    while(counter < 20 ){
    
      randomNumbers[counter] = (int)random(2,500);
        counter = counter + 1;
      }
    

    a quicker way of writing the final line is simply: counter++;

    http://processing.org/reference/Array.html

  • It sounds like you don't need that draw function at all.

    It also sounds like you need to assign the indexes of the array to values. Right now you're assigning a single int randomNumbers to be a random value 20 times, but you don't do anything with that value.

    Here is the Processing tutorial on array access: http://www.processing.org/reference/arrayaccess.html And here is one I wrote: http://StaticVoidGames.com/tutorials/basicConcepts/Arrays.jsp

  • edited February 2014
    // forum.processing.org/two/discussion/2978/
    // im-having-an-issue-with-creating-a-randomised-array-
    // in-setup-to-then-be-printed-out-in-draw
    
    static final int NUM = 20, RANGE = 01000;
    final int[] rndNums = new int[NUM];
    
    void setup() {
      size(400, 200, JAVA2D);
      frameRate(1.5);
      smooth(4);
    
      textSize(0100);
      textAlign(CENTER, CENTER);
    
      int idx = 0;
      while (idx != NUM)  rndNums[idx++] = (int) random(RANGE);
    }
    
    void draw() {
      background(-1);
      fill((color) random(#000000));
    
      final int idx = frameCount % NUM;
    
      text("Index #" + nf(idx, 2) + "\nValue #"
        + nf(rndNums[idx], 3), width>>1, height>>1);
    }
    
Sign In or Register to comment.