2013年9月8日星期日

On the X in the cycle between 0 and 9 code

int x = 0;
while(true){
x = (x+1)%10;
System.out.println("x:"+x);
}

see this code in the book and, indeed, to achieve a random number from 0 to 9 .
But I was very surprised , really did not understand.
At first glance I thought it would arguably cycle of 1234567890 .
but it actually can be randomly output 0 to 9. Which cheese explain
------ Solution ----------------------------------- ---------
so obvious rule , the output is not random , and the random number nothing


x:1
x:2
x:3
x:4
x:5
x:6
x:7
x:8
x:9
x:0
x:1
x:2
x:3
x:4
x:5
x:6
x:7
x:8
x:9

------ Solution ------------------------------------- -------
this is the remainder , the random number is generally used Math this class . . .
------ Solution ---------------------------------------- ----
not random, you put conditions change it,
while(x<9)
so look effect , you said that the random number , I have tried, indeed once from 3 starts , once from four starts , but I do not know why ?
------ Solution ---------------------------------------- ----
is starting from a cycle , is behind the numbers " obliterated " the
------ For reference only --------------- ------------------------
uh. . Is my own stupid X up . Missed a condition !

没有评论:

发表评论