简体   繁体   中英

Why does my random number only return 0?

I'm working in a coding class with IntelliJ Idea and I decided to make a project for fun. Here is my code. Note that java.util.Random is already imported:

    public static void generateForest(){
        int[][] forest = new int[50][50];
        Random ran = new Random();
        double randBounds = 150;
        double incr1 = 1.05;
        double incr2 = 1.07;
         for (int i = 0;i<50;i++){
            for(int q = 0;q<50;q++){
                try {
                    double ranNum = ran.nextInt(Math.abs((int) Math.round(randBounds)));
                    if (Math.round(ranNum)==0){
                        forest[i][q] = 0;
                    }
                    else {forest[i][q] = 1;}
                }
                catch(java.lang.IllegalArgumentException e){
                    forest[i][q] = 0;
                }
                if (q<=25){
                    randBounds = (randBounds/incr2);
                }
                else{randBounds = (randBounds*incr2);}
            }
            if (i <=25){
                randBounds = (randBounds/incr1);
            }
            else{randBounds = (randBounds*incr1);System.out.println(randBounds);}
        }
        printTrees(forest);
    }

printTrees just prints out the array input in the manner you see in the screenshots. The outputs generally looks like this:

产量

My code is supposed to essentially generate a sort of forest, with the trees being the Ts, and the bottom half is supposed to sort of mirror the top half. However, the bottom half is just entirely Ts. How can i fix it so that the bottom half is random in a similar pattern to the top half, only reversed?

As indicated by @SeanVanGorder in the comments, you're hitting the IllegalArgumentException case frequently, so you are always choosing zero for the "random" value. ( Sounds like an xkcd comic ).

The only thing in the try/catch block that would throw this exception is the call to Random.nextInt(int) , which is documented as throwing IllegalArgumentException if the parameter is not positive. So, your parameter, Math.abs((int) Math.round(randBounds)) is not positive.

The reason it isn't positive is it becomes zero. It's easy to see why:

  • Starting from 150
  • You divide it by 1.07 twenty-six times
  • You multiply it by 1.07 twenty-four times
  • So its value of randBounds after executing all 50 iterations of the for(int q = 0;q<50;q++){...} loop is 150 / 1.07 ^ 26*1.07^24 = 150 * 0.873 = 131
  • You then divide randBounds by 1.05, so it becomes 150 * 0.873 / 1.05 = 150 * 0.831 = 124.7 , and repeat these divisions/multiplations another 26 times (iterations of the outer loop). At the end of these 26 iterations, randBounds is just 1.25.
  • Then you repeat the inner loop (which makes randBounds 0.873 times the size per iteration, but are now multiplying by 1.05 instead of dividing by 1.05. But the overall change to randBounds per iteration is 0.873 * 1.05 = 0.917 , ie you're still decreasing its magnitude.

    Starting at 1.25 and decreasing the magnitude by 0.917 times, you're going to drop below 0.5 after just 10 iterations. As such, once you've done 36 iterations of the outer loop, randBounds is less than 1, and you'll always get the IllegalArgumentException .

However , you will start to get zeros all the time once 0.5 <= randBounds < 1.5 , since that rounds to 1, and Random.nextInt(1) always returns zero. So, actually, you will start getting all zeros earlier than that 36 iteration mark. In fact, this happens after 25 iterations of the outer loop.

It's hard to suggest a fix, because it's unclear what the correct behavior is; the only general fix is to recognize that unchecked exceptions (like IllegalArgumentException ) shouldn't be caught and swallowed, as they indicate programming errors.

perhaps a Gaussian distribution would be more to your liking:

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;

import java.util.Random;

public class StackOverflow45044823 {

  public static void populateField(final char[][] field) {
    Validate.noNullElements(field);
    final Random random = new Random();
    for (int rowNumber = 0; rowNumber < field.length; rowNumber++) {
      for (int columnNumber = 0; columnNumber < field[rowNumber].length; columnNumber++) {
        //random.nextGaussian() / 6.0 =  stdev=1/6 center=0
        //rowNumber / (float) field.length - 0.5 = grows uniformly from -0.5 to 0.5
        if (random.nextGaussian() / 6.0 < rowNumber / (float) field.length - 0.5) {
          field[rowNumber][columnNumber] = 'T';
        } else {
          field[rowNumber][columnNumber] = ' ';
        }
      }
    }
  }

  public static void main(String[] args) {
    final char[][] treeField = new char[50][50];
    populateField(treeField);
    printField(treeField);
  }

  private static void printField(char[][] field) {
    Validate.noNullElements(field);
    for (char[] aField : field) {
      System.out.println(StringUtils.join(aField, '|'));
    }
  }
}

It yields something like...

 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 
 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 
 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |T| | | | | | | | | 
 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 
 | | | | | | |T| | | | | | | | | | | | | | | | | | | |T| | | | | | | | | | | | | | | | | | | | | | 
 | | | | | | | | | | | | | | | | | | | | | | |T| | | | | | | | | | | | | | | | | | | | | | | | | | 
 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |T| | | | | 
 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 
 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 
 | | | | | | | | | | | | | |T| | | | | | |T| | | | | | | | | | | | | | | | | | | | | | | |T| | | | 
 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |T| |T| | | | | | | | 
 |T| | | | | | | | | | |T| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |T|T| 
 | | | | | | | | | | | | | | | |T| | | | | | | | | | | | | | | | | | | | | | | | | |T| | | | |T| | 
 | | | | | | | | | | | |T| | | | |T| | | | | |T| |T| | | | | | | | | | | | | | | | | | | | | | | | 
 | | | | | | | | | | | |T|T| | | | | | | | | | | | | | | | | | | | | | | | | | | | | |T| | | | | | 
 | | | | | | | | | | | | | | | | | | |T| | | | |T| | | | | | | | | |T| | | | |T| | | | | |T| | | | 
 | | | | | |T| | | | | | |T|T| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 
 | | | | | | | | |T|T|T| | | | |T| |T| | | | | | | | | | | | | | | | | | | | | | | | |T| | | | | | 
 | | | | |T| | | | |T| | |T| | | | | | |T| | | | |T|T|T| | | | | | | |T| | | | | |T| | | |T| | | | 
 | |T| | | |T| | | | | | | | |T| | | |T| | | | | | |T|T|T|T| | | | |T| | |T| | | | | | |T| | | | |T
T|T| | | | | | | | |T| | | | |T| | |T| | | | |T|T|T| | | | | | | | | | | |T| | | |T| |T| | | | | | 
 | | |T| |T| | | | | |T| | | | | | | | |T| | | | | |T| |T|T| | | |T| | | | |T| | | |T| | |T| |T| | 
T| | |T|T|T|T| |T|T|T| |T| | |T| |T| |T|T| | |T|T| | | |T|T| |T|T| |T|T|T| | |T| | |T|T| |T| | | |T
T| |T|T| | | |T|T| | | | | |T|T| | | |T| | | | | |T|T|T| |T| |T| | |T|T|T| |T|T|T|T| | | | | |T| | 
T| | | |T|T| | |T|T| |T| | | | | |T|T|T|T|T| | | |T|T| | | | | | |T|T| | | | | |T| | | |T|T| | | |T
T|T| | |T| |T| | |T| | |T| | | | | |T| |T|T| | | |T|T|T| |T|T|T| | |T| |T| |T| | | | | |T|T|T|T| |T
 | |T| |T|T|T|T| |T| |T|T| |T|T| |T| | |T|T|T|T| |T| |T| |T| |T| |T|T|T| |T| | |T|T| |T| |T| |T|T| 
 | |T| | | |T|T|T| |T| |T| |T| |T| |T|T| |T| |T|T| | |T|T|T|T| | | | |T|T|T|T|T| |T| |T|T|T| |T|T| 
 |T|T|T| |T|T| | | |T|T|T| |T|T|T| |T|T| | |T|T| | | |T| | |T|T| |T|T|T|T| | | |T|T| |T|T|T| |T| |T
T|T|T| |T|T| |T| |T| | |T|T|T|T| |T|T|T|T|T| |T|T|T|T|T|T|T|T|T|T|T|T|T|T|T| |T| |T| |T|T|T|T| |T|T
T|T|T|T|T| |T|T|T|T|T| | |T|T|T| |T| |T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T| |T| |T| |T|T| |T|T|T|T| |T|T
T|T|T|T|T|T| |T|T|T| |T| |T|T| |T|T|T|T|T|T| | |T|T|T|T|T|T|T|T|T|T|T|T| |T|T|T|T| |T| |T|T|T|T|T|T
T|T|T|T|T| |T|T|T|T|T| |T|T|T|T|T|T|T|T|T|T|T| |T|T|T|T|T| |T|T|T|T|T| | |T|T|T|T|T| |T| |T|T|T|T| 
T|T| |T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T| |T|T|T|T|T|T| |T|T|T|T|T|T|T| |T|T| |T|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T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T
T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T
T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T
T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T
T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T
T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM