android - Java random number generator not working -
my random number generator not working , don't understand it. new android programming , can not figure out peace of code. have tried several ways , not work properly. please help me out, new please not flame me.
examples: of actual code production
first set of trees spawned 1011010110
second set of trees spawned within same game 100001111110011
import java.util.random; import android.util.log; public class tree { public tree(int addition) { spawn = (int)(math.random() * 2); }
i have have tried.
private random random = new random(); public tree(int addition) { int randomnum = random.nextint(2); spawn = randomnum; }
thanks looking over.
steve
i'm not sure code you're referring producing outputs line of code homecoming either 0 or 1...
spawn = (int)(math.random() * 2);
something homecoming number between 0 , 9
spawn = (int)(math.random() * 10);
this happens because math.random() function returns double value positive sign, greater or equal 0.0 , less 1.0.
-- per http://docs.oracle.com/javase/7/docs/api/java/lang/math.html#random()
*edit - looks output you'd expect although sec set has suspicious looking repetition... if you're outputting both of these
private random random = new random(); public tree(int addition) { int randomnum = random.nextint(2); // 0 or 1 spawn = randomnum; // not random }
i think you're seeing repetition in sec tree. prepare can remove spawn
variable because you're doing reassigning in case.
java android random
No comments:
Post a Comment