PDA

View Full Version : Bug?


Kimchee
11-08-2008, 10:57 PM
I have written a basic program for my students in which I give them a randomly generated target percentage and they have to try and select that percentage of items and hit spacebar. However when there are only a few objects on the screen it generates percentages that are impossible to obtain. For example I had only two objects and the target percentage was 200%.

If someone could take a quick glane at the code and see where it is bugging out I would love it because everything I have tried has not found the bug...

pj-co
11-08-2008, 11:37 PM
this line:


target = 100*Math.round((Math.random()*itemsTotal+1))/itemsTotal;


should be something like:


target = ( 100 * Math.ceil (Math.random()*itemsTotal ) ) / itemsTotal;


if you think about it when you have only 2 items and Math.random gives you a result of 1, then you add 1, multiply it by the number of items, that will give you a wacky percent.

Kimchee
11-08-2008, 11:41 PM
So simple yet so obvious...;)

Thanks!