This site requires JavaScript, please enable it in your browser!
Greenfoot back
erdelf
erdelf wrote ...

2012/7/12

n must be positive?

erdelf erdelf

2012/7/12

#
I got this error
Terminal Window wrote...
java.lang.IllegalArgumentException: n must be positive at java.util.Random.nextInt(Random.java:300) at greenfoot.Greenfoot.getRandomNumber(Greenfoot.java:145) at Level.actL(Level.java:81) at Level.act(Level.java:67) at greenfoot.core.Simulation.actWorld(Simulation.java:571) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
line81:
 if(Greenfoot.getRandomNumber((time) * 2) >= 10) 
time is:
int time = (int)System.currentTimeMillis() - (int)startTime;
startTime is set at the beginning of the scenario:
private double startTime = System.currentTimeMillis();
danpost danpost

2012/7/12

#
I am not certain, but I think if you change the assignment of 'time to:
int time = (int) (System.currentTimeMillis - startTime);
you will avoid the error. Taking the int of each individually will cause that error when an overflow (of int) occurs (the 'endTime' becomes less than the 'startTime'). However, I am not sure if line 81 is what you actually want. It says 'wait 5 milliseconds before possibly doing whatever', and the chance of it doing whatever will be at 90 percent after just one second.
erdelf erdelf

2012/7/12

#
thx, for that. Why only 5 miliseconds?o oh and what is n?
danpost danpost

2012/7/12

#
That is what the 'System.currentTimeMillis()' returns; its value is incremented 1000 times every second (so actually, it would be a 99.5 percent chance of doing whatever after just one second; ((2000 - 10)/2000 * 100percent). After 6 milliseconds, time will be 6; which makes the if clause read as 'if (Greenfoot.getRandomNumber(12) >= 10)', having a 2 in 12 chance of doing whatever there (or 16.67 percent; (12 - 10)/12 * 100percent). After one second, 'time' will be 1000, which makes the if clause read as 'if (Greenfoot.getRandomNumber(2000) >= 10)'
danpost danpost

2012/7/12

#
'n' is the parameter for 'Greenfoot.getRandomNumber(int n)'.
erdelf erdelf

2012/7/12

#
oh ok, I forget that the difference is in milliseconds
You need to login to post a reply.