Java 随机。nextInt(1);只返回1
这是我的minecraft mod算法。出于某种原因,似乎Java 随机。nextInt(1);只返回1,java,random,minecraft,Java,Random,Minecraft,这是我的minecraft mod算法。出于某种原因,似乎rand.nextInt(1)仅返回1。为什么? public void generate(){ Random rand = new Random(); Arrays.fill(wc,null); int c = generateCat();//Generates a int of 1-16 randomly int i = 0; int xi = 0; int x = getCoordX(
rand.nextInt(1)代码>仅返回1。为什么?
public void generate(){
Random rand = new Random();
Arrays.fill(wc,null);
int c = generateCat();//Generates a int of 1-16 randomly
int i = 0;
int xi = 0;
int x = getCoordX();
int y = getCoordY();
int maxc = 0;
boolean d1 = true;
boolean d2 = false;
boolean d3 = false;
boolean d4 = false;
boolean loop = true;
wc[0] = new WeatherChunk(world,x,y,c);//Here the starting object is generated
x=x-1;
if(rand.nextInt(1)==1){//Here the c value (is supposed to have a 50% chance of reducing) But it never does EVER!?
c=getHNChunkCat(x,y);
c--;
wc[1] = new WeatherChunk(world,x,y,c);//Here the c value (is supposed to have a 50% chance of reducing) But it never does EVER!?
}else{
c=getHNChunkCat(x,y);
wc[1] = new WeatherChunk(world,x,y,c);
}
x=x+2;
if(rand.nextInt(1)==1){
c=getHNChunkCat(x,y);
c--;
wc[2] = new WeatherChunk(world,x,y,c);
}else{
c=getHNChunkCat(x,y);
wc[2] = new WeatherChunk(world,x,y,c);
}
x=x-1;
y++;
if(rand.nextInt(1)==1){
c=getHNChunkCat(x,y);
c--;
wc[3] = new WeatherChunk(world,x,y,c);
}else{
c=getHNChunkCat(x,y);
wc[3] = new WeatherChunk(world,x,y,c);
}
y=y-2;
if(rand.nextInt(1)==1){
c=getHNChunkCat(x,y);
c--;
wc[4] = new WeatherChunk(world,x,y,c);
}else{
c=getHNChunkCat(x,y);
wc[4] = new WeatherChunk(world,x,y,c);
}
y=wc[0].coordY;
x=wc[0].coordX-2;
i=5;
while(loop){
d1=true;
while(d1){
if(chunkExists(x+1,y)){
if(getHNChunkCat(x,y)>maxc)maxc=getHNChunkCat(x,y);
if(rand.nextInt(1)==1){
c=getHNChunkCat(x,y);
c--;
wc[i] = new WeatherChunk(world,x,y,c);
}else{
c=getHNChunkCat(x,y);
wc[i] = new WeatherChunk(world,x,y,c);
}
x++;
y++;
i++;
}else{
d1=false;
}
}
d2=true;
while(d2){
if(chunkExists(x,y-1)){
if(getHNChunkCat(x,y)>maxc)maxc=getHNChunkCat(x,y);
if(rand.nextInt(1)==1){
c=getHNChunkCat(x,y);
c--;
wc[i] = new WeatherChunk(world,x,y,c);
}else{
c=getHNChunkCat(x,y);
wc[i] = new WeatherChunk(world,x,y,c);
}
x++;
y--;
i++;
}else{
d2=false;
}
}
d3=true;
while(d3){
if(chunkExists(x-1,y)){
if(getHNChunkCat(x,y)>maxc)maxc=getHNChunkCat(x,y);
if(rand.nextInt(1)==1){
c=getHNChunkCat(x,y);
c--;
wc[i] = new WeatherChunk(world,x,y,c);
}else{
c=getHNChunkCat(x,y);
wc[i] = new WeatherChunk(world,x,y,c);
}
x--;
y--;
i++;
}else{
d3=false;
}
}
d4=true;
while(d4){
if(chunkExists(x-1,y)){
if(getHNChunkCat(x,y)>maxc)maxc=getHNChunkCat(x,y);
if(rand.nextInt(1)==1){
c=getHNChunkCat(x,y);
c--;
wc[i] = new WeatherChunk(world,x,y,c);
}else{
c=getHNChunkCat(x,y);
wc[i] = new WeatherChunk(world,x,y,c);
}
x--;
y++;
i++;
if(chunkExists(x,y)){
d4=false;
break;
}
}else{
d4=false;
}
}
y=wc[0].coordY;
x=wc[0].coordX;
xi=0;
while(true){
if(!chunkExists(x-xi,y)){
x=x-xi;
break;
}else{
xi++;
}
}
if(maxc==0){
loop=false;
}
}
cleanChunks();
WEATHER.addChunkArray(wc2);
}
我也试过了
wc[1] = new WeatherChunk(world,x,y,(getHNChunkCat()-rand.nextInt(1)));
您调试得不够,因为这是不可能的。确实,您只会收到一个值,但它是0
,而不是1
发件人:
返回一个伪随机、均匀分布的int值,该值介于0(包括0)和指定值(独占)之间
如果你想要50%的机会,我建议你把它转换成nextInt(2)
或者像@Flynn1179建议的那样:。你想得到一个介于0和1之间的小数点吗?使用以下命令:
Math.random() // returns a random double
nextInt
方法仅返回介于0和指定参数之间的随机数,但不包括该参数。因此,您应该总是得到0。nextInt(x)
返回int
介于0
包含和x
排除之间的值。nextInt
方法的参数是上参数。所以它是一个介于0和1之间的整数范围。它只返回0,从不返回1是的,我同意。我是从哪里来的?一个是排他性的。不需要nexttint(2)
,只需使用nextBoolean()
谢谢你,但我很确定我已经在另一个地方得到了同样的东西,时间来检查并修复所有事情。那一定是另一种情况,因为不可能按照你在问题中提出的方式来做。