简体   繁体   中英

Java histogram from random generated numbers

I am playing with and learning a bit of java, so I'm really a newbi e ... My problem is - I am generating 5 random numbers from 1 to 5. My program then calculates how many times are number 1, number 2, number 3, number 4 and number 5 generated within these randoms.

public static void main(String[] args) {
    Random rand = new Random();
    int[] array = new int[5];
    int randomNumber;
    int i;
    int p1 = 0;
    int p2 = 0;
    int p3 = 0;
    int p4 = 0;
    int p5 = 0;
    System.out.println("Random numbers:");
    for (i = 0; i < 5; i++) {
        randomNumber = rand.nextInt(5) + 1;
        System.out.print(randomNumber);
        if (i < 4) {
            System.out.print(", ");
        }

        if (randomNumber == 1) {
            array[0] = p1++;
        } else if (randomNumber == 2) {
            array[1] = p2++;
        } else if (randomNumber == 3) {
            array[2] = p3++;
        } else if (randomNumber == 4) {
            array[3] = p4++;
        } else if (randomNumber == 5) {
            array[4] = p5++;
        }
    }
    //výpis četnosti
    System.out.println();
    System.out.println();
    System.out.println("Histogram: "); 

    for (i = 0; i < array.length; i++) {
        System.out.println("Number " + (i + 1) + ": " + array[i] + ".");
    }
}

The programme acts strange for my understanding. The output is always displaying the real count of the number contained in radom generated bundle exactly minus 1 and I really don't understand why. ... So if there are three times generated number 3's from random generator, my programme show for number three only count "2".

I'd be really grateful for helping me with this. Thanks.

array[0] = p1++;

Is the same as:

array[0] = p1;
p1 = p1 + 1;

I hope that is enough for you to spot your issue.

You're post-incrementing your count variables. So, what you assign to your int array bucket the very first time you count is 0.

Instead, you can simplify this by just incrementing the integers in your array like so:

public static void main(String[] args) {
    Random rand = new Random();
    int[] array = new int[5];
    int randomNumber;
    int i;

    System.out.println("Random numbers:");
    for (i = 0; i < 5; i++) {
        randomNumber = rand.nextInt(5) + 1;
        System.out.print(randomNumber);
        if (i < 4) {
            System.out.print(", ");
        }

        array[randomNumber - 1]++;
    }
    //výpis četnosti
    System.out.println("\n\nHistogram: "); 

    for (i = 0; i < array.length; i++) {
        System.out.println("Number " + (i + 1) + ": " + array[i] + ".");
    }
}

Running this gives me the following output:

Random numbers:
2, 3, 5, 3, 2

Histogram: 
Number 1: 0.
Number 2: 2.
Number 3: 2.
Number 4: 0.
Number 5: 1.

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