Computer Science Canada help with Rand.Int if statements |
Author: | sci23 [ Sat Nov 24, 2007 9:46 pm ] | ||
Post subject: | help with Rand.Int if statements | ||
i need help on my program...the if statements for the Rand.Int don't work(the numbers get screwed up)....so how can i make them work
|
Author: | Saad [ Sat Nov 24, 2007 10:03 pm ] |
Post subject: | RE:help with Rand.Int if statements |
2 things: 1: You could specify the location of the error 2: When using variables like c, it helps to tell in comments what it is. But anyway, looking through the code I think the error is in the if statements. Everytime you goto an if-statement a new random number is generated, you should probably store it in a variable |
Author: | HeavenAgain [ Sat Nov 24, 2007 10:13 pm ] |
Post subject: | RE:help with Rand.Int if statements |
loop var myRandomNumber : int := Rand.Int(1,6) put "you rolled ",myRandomNumber end loop you dont need an array, and you dont have to make a variable for 1 to 6, one variable to keep track of the random is enough and the if statement is not necessary |
Author: | Wolf_Destiny [ Sun Nov 25, 2007 11:00 pm ] | ||
Post subject: | Re: RE:help with Rand.Int if statements | ||
Saad @ Sat Nov 24, 2007 10:03 pm wrote: But anyway, looking through the code I think the error is in the if statements. Everytime you goto an if-statement a new random number is generated, you should probably store it in a variable
That is in fact the exact error in your program. Now I do agree with HeavenAgain that you don't need an array, however you should probably get it working and discover the problem before optimizing your code. So in order to fix your problem, before the line "die(Rand.Int(1,6))" add a variable and define its value as Rand.Int(1,6). Then replace each instance of die(Rand.Int(1,6)) with die(YOUR_VAR_NAME_HERE) Doing the same with d you'll get a bit of code much like the following which runs without error.
(Variable names that I used were ROLLED and ROLLED2 to make the changes more obvious) Hope that helps! |
Author: | Tony [ Sun Nov 25, 2007 11:37 pm ] | ||||
Post subject: | RE:help with Rand.Int if statements | ||||
It should probably be noted that
is equivalent to
|