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 3153 times)
Random numbers help!
Jul 7th, 2009, 6:45am
 
I need to recreate this.

img4 . imageshack . us / img4 / 6153 / picture1hxj . png

The program is supposed to start with 1-9. Then if you click on the canvas you get a sequence of 9 consecutive numbers but with a random start. The maximum random start is 91.

Could anyone help me out with this or give me some help in working out how to do it?
Re: Random numbers help!
Reply #1 - Jul 7th, 2009, 6:46am
 
I couldn't do a hyperlink but if you takeaway the spaces then you can see the picture. I am sure it's really easy but I am new to processing!
Re: Random numbers help!
Reply #2 - Jul 7th, 2009, 8:59am
 
That's http://img4.imageshack.us/img4/6153/picture1hxj.png

Where are you stuck? If you look at the reference page, you will need random(), loadFont(), textFont() and text(), rectangle(), mousePressed() and some other elements, but it should get you started.
Re: Random numbers help!
Reply #3 - Jul 7th, 2009, 9:03am
 
Oh okay, thank you very much. I couldn't make a hyperlink because I'm new on this forum. I didn't have enough posts. I'll give those references a look at. Much appreciated!
Re: Random numbers help!
Reply #4 - Jul 7th, 2009, 9:06am
 
link to image

random is easy enough - you can set limits to ensure you get a number between 1 and 91 (note that random returns a float so you'll want to cast or convert the result - though be careful since rounding operations may affect the values that are returned).

Then you just need to set up a for loop to populate the table.  Look at the reference for how to implement text etc...
Re: Random numbers help!
Reply #5 - Jul 7th, 2009, 9:16am
 
This is what I will have to make.

sendspace.com/file/z1znot
Re: Random numbers help!
Reply #6 - Jul 7th, 2009, 12:32pm
 
So that is what I have to make. I'm still finding it hard. Can anyone look at the program and see how hard they thing it is to make? I've done a bit of it but can't complete it.
Re: Random numbers help!
Reply #7 - Jul 7th, 2009, 1:36pm
 
show us the code you have and we will help.

start by drawing the squares - should be a case of just calling rect() 9 times.
Re: Random numbers help!
Reply #8 - Jul 7th, 2009, 2:37pm
 
2 loops and one rect() Smiley
DO IT!
Re: Random numbers help!
Reply #9 - Jul 8th, 2009, 9:28am
 
(i had loop unrolling turned on 8) )
Re: Random numbers help!
Reply #10 - Jul 9th, 2009, 1:16am
 
I threw this together the other night just for the hell of it, but didn't post it as I had a slight doubt about the the random numbers which I hadn't had time to double-check.

I used:

int randomNum = (int) random(1,101);

and for some reason had doubts as to whether that really would return random numbers between 1 and 100.  From the documentation I knew it should - but wondered whether the cast would have any effect on the outcome  I now feel a bit silly  Embarrassed

I'm not posting source as yet - this smells like homework.  Let's see how the OP gets on before giving too much away Wink
Re: Random numbers help!
Reply #11 - Jul 9th, 2009, 3:31am
 
You are right on the homework smell, I think... :-P

And I think it is right: (int) on positive numbers just truncate before the decimal point, so you should get numbers between 1 and 100, inclusive.
Of course, you can just generate some thousands of numbers and check the bounds (what min and max you got)...  Cool
Re: Random numbers help!
Reply #12 - Jul 9th, 2009, 4:17am
 
haha you smell right. i've got the standard data

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

 fill(0, 255, 6);
rect(0, 180, 90, 90);
fill(0);
text("7", 32, 240);

 fill(255, 255, 0);
rect(90, 0, 90, 90);
fill(0);
text("2", 122, 60);

 fill(0, 255, 6);
rect(90, 90, 90, 90);
fill(0);
text("5", 122, 150);

 fill(255, 255, 0);
rect(90, 180, 90, 90);
fill(0);
text("8", 122, 240);

 fill(0, 255, 6);
rect(180, 0, 90, 90);
fill(0);
text("3", 212, 60);

 fill(255, 255, 0);
rect(180, 90, 90, 90);
fill(0);
text("6", 212, 150);

 fill(0, 255, 6);
rect(180, 180, 90, 90);
fill(0);
text("9", 212, 240);
}

but this is probably wrong. but i am just finding it hard to link the text to the random numbers. i understand the logic behind it.

the fact that it has to start with the numbers 1-9 and the maximum number it can start with is 91. but its just implementing the code!
Re: Random numbers help!
Reply #13 - Jul 9th, 2009, 7:15am
 
It's not 'wrong' inasmuch as it works, but it isn't the most efficient approach.  Before going any further see whether, instead of manually writing each text block, you can recreate your square using two for loops.  Actually if you're a real beginner first try to recreate each line with a single for loop:

Code:
for(int i=0; i<3; i++) {
   // code goes here.
   // you can use the value of i to determine
   // both the number displayed in text and the position
   // of the rect.
 }


Look at your rect and text positions and see how you might recreate them by using multiplication of 'i'.  Once you've figured that out move on to creating the whole square with two nested loops:

Code:
for(int i=0; i<3; i++) {
 for(int j=0; j<3; j++) {

   // code goes here.
   // you can use the value of i and j to determine
   // both the number displayed in text and the position
   // of the rect.

 }
}


That's a much more efficient way of doing things - especially in terms of the amount of code you have to write!  Once you get the hang of these structures you will find them incredibly useful.  This approach will also make it much easier to move onto the next step Wink
Re: Random numbers help!
Reply #14 - Jul 12th, 2009, 4:28pm
 
I am still really struggling with it. It's in for Wednesday and I don't see how I am going to get it done!
Pages: 1 2 3