2015-01-20 136 views
-1

這是我的Minecraft mod算法。出於某種原因,似乎rand.nextInt(1);只返回1。爲什麼?Random.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))); 
+3

'nextInt(x)'返回一個介於'0'和''***'獨佔***之間的'int'。 – clcto 2015-01-20 20:49:39

+1

'nextInt'方法的參數是上面的參數。所以它是0到1之間的整數範圍。 – 2015-01-20 20:50:11

+3

它只返回0,從不1 1 – njzk2 2015-01-20 20:50:15

回答

10

您還沒有足夠的調試,因爲那是不可能的。確實,您只會收到一個值,但它是0,而不是1

the docs

返回一個僞隨機,均勻分佈的int值介於0(含)和指定值(獨家

如果你想有一個50%的機會,我建議你把它變成nextInt(2)或@ Flynn1179建議:nextBoolean()

+7

不需要'nextInt(2)',只需使用'nextBoolean()' – Flynn1179 2015-01-20 20:52:25

+0

謝謝,但我很確定,我已經得到了同樣的東西在另一個地方工作時間去解決所有問題 – jtsfour 2015-01-20 21:29:35

+0

然後它必須已經是一種不同的情況,因爲不可能像你在你的問題中列出的那樣去做。 – 2015-01-20 21:30:20

2

你想獲得0到1之間的小數嗎?使用此:

Math.random() // returns a random double 

nextInt方法只返回0之間,並指定參數的隨機數,但不包括參數。因此,你應該總是得到0.