We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Random numbers help!
Pages: 1 2 3 
Random numbers help! (Read 3154 times)
Re: Random numbers help!
Reply #15 - Jul 12th, 2009, 11:13pm
 
Show the code you have done so far, ask specific questions, we will answer.
Re: Random numbers help!
Reply #16 - Jul 13th, 2009, 2:30am
 
I've shown the code that I have done before but it's not the most efficient way of writing the code like the other guy said. If you look at the code that I've already written you'll see what the program has to look like. But like the other guy said you can condense it so that there is less code and it's more efficient. Then I need help with making the random numbers, like said at the start of the topic.

I understand the concept of the program, I am just having trouble implementing it.
Re: Random numbers help!
Reply #17 - Jul 13th, 2009, 3:48am
 
The problem with programmers like me, is that it is difficult to see where you can be stuck.
You were given a link (and example) to random(), so you can draw a number between 1 and 91 with int(random(1, 92)).
You can build the interface with the for loops, as blindfish shown. Have a variable to hold the number, increment it on each loop, starting from the first drawn number, use the variable in the text() call.
That's mostly a bit of simple math (to compute the positions) and logic.
Re: Random numbers help!
Reply #18 - Jul 13th, 2009, 8:37am
 
you don't need random numbers, you just need a random number, one, all the others are increments of this.

change your code to draw the boxes in numeric order.

add a int variable outside of setup(). call it randomNum.

set it to a value inside setup(). any value will do. 6.

change the bits where you print the digit in each of the boxes to print the variable instead. like this:

text(randomNum, 32, 240); // these numbers are just an example

at the bottom of each block that draws the box increment the variable.

randomNum++;

check that the boxes are now numbered 6, 7, 8, 9, 10, 11, 12, 13, 14

now change the randomNum in setup to be random. hit refresh. hit refresh again, watch it change.
Re: Random numbers help!
Reply #19 - Jul 14th, 2009, 9:32am
 
int = RandomNum;

void setup(){
 
 RandomNum = 6
 
 size(270, 270);
 PFont font;
 font = loadFont("CourierNewPS-BoldMT-48.vlw");
 textFont(font);
 
 fill(0, 255, 6);
rect(0, 0, 90, 90);
 fill(0);
 text(RandomNum, 32, 60);
 randomNum++;
 
   fill(255, 255, 0);
rect(90, 0, 90, 90);
fill(0);
text(RandomNum, 122, 60);
randomNum++;

fill(0, 255, 6);
rect(180, 0, 90, 90);
fill(0);
text(RandomNum, 212, 60);
randomNum++;

 fill(255, 255, 0);
rect(0, 90, 90, 90);
fill(0);
text(RandomNum, 32, 150);
randomNum++;

fill(0, 255, 6);
rect(90, 90, 90, 90);
fill(0);
text(RandomNum, 122, 150);
randomNum++;

 fill(255, 255, 0);
rect(180, 90, 90, 90);
fill(0);
text(RandomNum, 212, 150);
randomNum++;

 fill(0, 255, 6);
rect(0, 180, 90, 90);
fill(0);
text(RandomNum, 32, 240);
randomNum++;

 fill(255, 255, 0);
rect(90, 180, 90, 90);
fill(0);
text(RandomNum, 122, 240);
randomNum++;

 fill(0, 255, 6);
rect(180, 180, 90, 90);
fill(0);
text(RandomNum, 212, 240);
randomNum++;
}



This what I have done so far. How close am I to being correct?

Thank you for your help so far, you have been very helpful.
Re: Random numbers help!
Reply #20 - Jul 14th, 2009, 10:25am
 
declaring a variable is simply:

int randomNum;

and everywhere after that has to use *exactly* the same name - randomNum. 'RandomNum' won't work because of the capital R.

the call to size() needs to be the first line of your setup(). this is a processing thing.

and where you initilise randomNum in setup is missing a ; at the end - the compile error tells you exactly this.

other than that it runs and produces pretty much what you want.

next up (after you've made those changes) we can move onto the 'update on click' thing.

look up mousePressed() and draw() and noLoop() in the reference...
Re: Random numbers help!
Reply #21 - Jul 15th, 2009, 3:27am
 
Yes, I have made those changes and it works perfectly fine. So now I basically want to be able to click anywhere on the canvas and the numbers to change randomly. But for the first square to go up to a maximum of 91.
Re: Random numbers help!
Reply #22 - Jul 15th, 2009, 5:20am
 
cool.

the next thing is that to be able to redraw the image it has to be in a draw() method, not in setup() - setup runs once, draw runs many times.

everything up to and including the call to textfont() should remain in setup(). so add a new line with a closing curly bracket underneath it.

then a blank line and then

void draw() {

which means that the rest of your file, the drawing bit, will run continuously.

BUT you don't want it to run continually, only when the mouse is pressed. so, at the bottom, add noLoop() which will stop it repeating.

as for the random start number, have a look back through this thread (blindfish's post specifically) and read the reference page and change the randomNum = 6; in the setup() code to use a random number instead. you should now get a different sequence each time you run it.

then read the mousePressed() and noLoop reference pages and see if you can work out what comes next.
Re: Random numbers help!
Reply #23 - Jul 15th, 2009, 5:51am
 
Yes I have done that so it does a random number. But I need for the program to start with the number 1 and then everytime you press on the canvas it will change.

I am struggling with what code to put with the mousepressed. It's obviously got to be whenever you click, all the numbers will change.

I guess to keep the boundaries of 1-91 it would be....

int RandomNum = (random(1,91));

but its putting that kind of code with the mousepressed i am struggling with.
Re: Random numbers help!
Reply #24 - Jul 15th, 2009, 8:25am
 
Initialialize RandomNum at declaration time:
int RandomNum = 1;
When drawing, you just use it:
text(RandomNum++, 32, 60);
text(RandomNum++, 122, 60);

(or the equivalent in the loop if you have done it already).
In the mousePressed() function, reset this variable to the random value:
RandomNum = int(random(1,91));
redraw(); // To invoke draw()
Re: Random numbers help!
Reply #25 - Jul 15th, 2009, 9:13am
 
int RandomNum;

void setup(){
 
 size(270, 270);
 PFont font;
 font = loadFont("CourierNewPS-BoldMT-48.vlw");
 textFont(font);

 int RandomNum = 1;
}
 
 void draw() {

 fill(0, 255, 6);
rect(0, 0, 90, 90);
 fill(0);
 text(RandomNum++, 32, 60);
 
   fill(255, 255, 0);
rect(90, 0, 90, 90);
fill(0);
text(RandomNum++, 122, 60);

fill(0, 255, 6);
rect(180, 0, 90, 90);
fill(0);
text(RandomNum++, 212, 60);

 fill(255, 255, 0);
rect(0, 90, 90, 90);
fill(0);
text(RandomNum++, 32, 150);

fill(0, 255, 6);
rect(90, 90, 90, 90);
fill(0);
text(RandomNum++, 122, 150);

 fill(255, 255, 0);
rect(180, 90, 90, 90);
fill(0);
text(RandomNum++, 212, 150);

 fill(0, 255, 6);
rect(0, 180, 90, 90);
fill(0);
text(RandomNum++, 32, 240);

 fill(255, 255, 0);
rect(90, 180, 90, 90);
fill(0);
text(RandomNum++, 122, 240);

 fill(0, 255, 6);
rect(180, 180, 90, 90);
fill(0);
text(RandomNum++, 212, 240);

void mousePressed() {
 int RandomNum = int(random(1,91));
redraw();
}

noLoop()




This is what I have just done. I feel like I am very close to completing this now. Just need to create the mousepressed code to be created. I'm not to sure how to go about it really.
Re: Random numbers help!
Reply #26 - Jul 15th, 2009, 9:37am
 
You are almost there. Remember to close the draw() function (add a closing brace }) before the mousePressed() function.
And put this noLoop() inside setup(). And remove the double declaration of RandomNum from there too: the = 1 must be on the first line.
Re: Random numbers help!
Reply #27 - Jul 15th, 2009, 9:51am
 
nearly there, yes. your mousepressed looks fine (but see below)

your noLoop() is outside of the draw(), it needs to be inside.

and you're missing the closing } for draw(), should be before your mousePressed() method.

(actually you can get away with it not being there but your code will keep redrawing very fast eating up processor cycles. you won't see any change until you hit the mouse and then it'll start drawing the new numbers very fast, again and again. no point updating the screen if it hasn't changed)

and your
int RandomNum = 1;
and
int RandomNum = int(random(1,91));

is wrong...

int variable = 8;

defines and sets the variable. you only need to define the variable once (at the top there)

variable = 4; or variable = (int)random...

is enough to change the value. by using int you now have 3 different things all called RandomNum but with different scopes... just get rid of the int before RandomNum in setup and mousePressed and just leave it before the top one.

(the convention, btw, is for variable names to start with a lower case letter - 'variableName' and leading uppercase names are used for classes - ClassName. and full capitals are for CONSTANTS)

and where you have blocks of code that's identical apart from the odd number, that's generally a sign that you should make that code either a method or a class... you probably won't gain much with such small repeated blocks but...

and finally, where you are using the same numbers over and over again (90, 180, 32, 150...) it is generally a good idea to replace them with a constant.

oh, didn't see PhiLho's post there 8) actually, he appears to be to blame for the scope problems and the variable name 8)
Re: Random numbers help!
Reply #28 - Jul 15th, 2009, 11:59am
 
koogy wrote on Jul 15th, 2009, 9:51am:
oh, didn't see PhiLho's post there 8) actually, he appears to be to blame for the scope problems and the variable name 8)
Smiley
I kept the variable name as it was above... I still use initial uppercase letter for my function names (an habit from C programming time on Windows), something some Java people frown upon, so I don't lecture people on their capitalization habits (but it is still a good idea to explain the common practice, that's just not me doing that! Wink).

And I advised to initialize at declaration time, ie. at the top, not elsewhere.
Re: Random numbers help!
Reply #29 - Jul 21st, 2009, 1:43am
 
Sorry - after saying "I can do this" I didn't contribute much help; but I was on holiday Smiley

Looks like the OP got there in the end, though I wonder whether it would have been simpler to draw the initial square in setup and thus avoid the need for noLoop().  Also a for loop or two would have avoided duplication of code.  Here's a function to draw the grid using 3 for loops:

Code:
// This function takes a single argument: the start number for the grid
void drawGrid(int inputNum) {
      textAlign(CENTER);
   
   // Draw the first line
   // the for loop repeats 3 times with j equal to 0 then 1 then 2.
   for(int j=0; j<3; j++) {
       // % (modulo) gives you the remainder of a division
       // so if inputNum % 2 == 0 it is even.  This condition can then
       // be used to alternate the background fill on odd/even squares.
       if(inputNum % 2 ==0) {
         fill(230,230,0);
       }
       else {
         fill(0,200,0);
       }
       // Avoid magic numbers.  by using width/3 and height/3 this will always
      // work regardless of the dimensions set in size()
      // Notice that the x position uses multiples of j so each time through the loop
      // the x position will be shifted:
      // x = 0*(width/3) = 0
      // x = 1*(width/3) = 90
      // x = 2*(width/3) = 180
       rect(j*(width/3), 0, width/3, height/3);
       fill(0);
       // the position of the text is calculated in a similar way, with an offset added to
       // place it in the middle of the square (note that textAlign(CENTER) is useful in this case)
       text(inputNum++, j*(width/3) + width/6, height/5);
   }
   
   // Draw the second line
   // Notice that the only difference is that the height of the rectangles and text is now offset by height/3
    for(int j=0;j<3;j++) {
       if(inputNum % 2 ==0) {
         fill(230,230,0);
       }
       else {
         fill(0,200,0);
       }
       rect(j*(width/3), height/3, width/3, width/3);
       fill(0);
       text(inputNum++, j*(width/3) + width/6, height/3+ height/5);
   }
   
   // Draw the third line
   // Again notice that the only difference is that the height of the rectangles and text is offset by 2*(height/3)
   // This makes the 3 for loops ideal candidates for a nested loop
   for(int j=0;j<3;j++) {
       if(inputNum % 2 ==0) {
         fill(230,230,0);
       }
       else {
         fill(0,200,0);
       }
       rect(j*(width/3), 2*(height/3), width/3, width/3);
       fill(0);
       text(inputNum++, j*(width/3) + width/6, 2*(height/3)+ height/5);
   }
}


OK - so that isn't a great deal less code than the original 'manual' method; but it helps to explain how a nested loop works.  Here it is again with nested loops - there's now a lot less code:

Code:
void drawGrid(int inputNum) {
 textAlign(CENTER);
 // Nested loop
 // Just wrap the original loop with another.  In this case with each repetition of
 // the outer loop the inner loop is also repeated.  So:
 // i=0, j loop repeats 3 times with j equal to 0,1 and 2
 // i=1, j loop repeats 3 times with j equal to 0,1 and 2
 // i=2, j loop repeats 3 times with j equal to 0,1 and 2
 for(int i=0;i<3;i++){
   // the inner loop is run for each repetition of the outer loop
   for(int j=0;j<3;j++) {
       if(inputNum % 2 ==0) {
         fill(230,230,0);
       }
       else {
         fill(0,200,0);
       }
       // The only change to the original loop is to shift the y position by multiples of i
       // in the same way that the x position is shifted by multiples of j.
       // So for each iteration of the i loop the 3 boxes will be drawn at:
       // y = 0*(height/3) = 0
       // y = 1*(height/3) = 90
       // y = 2*(height/3) = 180
       rect(j*(width/3), i*(height/3), width/3, width/3);
       fill(0);
       // text y position also adjusted using multiples of i
       text(inputNum++, j*(width/3) + width/6, i*(height/3)+ height/5);
   }// end of inner loop
 }
   
}


OK - so with all the comments it looks like a lot of code so to prove the point here it is without them:

Code:
void drawGrid(int inputNum) {
 textAlign(CENTER);
 for(int i=0;i<3;i++){
   for(int j=0;j<3;j++) {
       if(inputNum % 2 ==0) {
         fill(230,230,0);
       }
       else {
         fill(0,200,0);
       }
       rect(j*(width/3), i*(height/3), width/3, width/3);
       fill(0);
       text(inputNum++, j*(width/3) + width/6, i*(height/3)+ height/5);
   }
 }
   
}


The really neat thing about the nested structure is that it doesn't just require less code but you can use it to generate a grid of any size.  So instead of setting the loop limits to 3 (and dividing width and height by 3) you could replace this value with a variable and set it to whatever number you want (though in this case you'd also need to tweak a couple of other things to make it position the text correctly).

So - whenever you find yourself repeating the same code over and over, just with different values, figure out the pattern in the values (i.e. do some basic math) and build a loop, or nested loop.  This will save you a lot of time in the long run!
Pages: 1 2 3