Java histogram from random generated numbers












0















I am playing with and learning a bit of java, so I'm really a newbie ... 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.










share|improve this question




















  • 1





    Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

    – Joe C
    Nov 20 '18 at 22:48






  • 1





    Read in the docs what the suffix ++ operator does and what it returns.

    – Michael Butscher
    Nov 20 '18 at 22:49
















0















I am playing with and learning a bit of java, so I'm really a newbie ... 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.










share|improve this question




















  • 1





    Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

    – Joe C
    Nov 20 '18 at 22:48






  • 1





    Read in the docs what the suffix ++ operator does and what it returns.

    – Michael Butscher
    Nov 20 '18 at 22:49














0












0








0








I am playing with and learning a bit of java, so I'm really a newbie ... 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.










share|improve this question
















I am playing with and learning a bit of java, so I'm really a newbie ... 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.







java histogram






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 22:54









weston

39.5k1696170




39.5k1696170










asked Nov 20 '18 at 22:46









Robert ZelenkaRobert Zelenka

31




31








  • 1





    Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

    – Joe C
    Nov 20 '18 at 22:48






  • 1





    Read in the docs what the suffix ++ operator does and what it returns.

    – Michael Butscher
    Nov 20 '18 at 22:49














  • 1





    Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

    – Joe C
    Nov 20 '18 at 22:48






  • 1





    Read in the docs what the suffix ++ operator does and what it returns.

    – Michael Butscher
    Nov 20 '18 at 22:49








1




1





Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

– Joe C
Nov 20 '18 at 22:48





Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.

– Joe C
Nov 20 '18 at 22:48




1




1





Read in the docs what the suffix ++ operator does and what it returns.

– Michael Butscher
Nov 20 '18 at 22:49





Read in the docs what the suffix ++ operator does and what it returns.

– Michael Butscher
Nov 20 '18 at 22:49












2 Answers
2






active

oldest

votes


















0














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("nnHistogram: ");

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.





share|improve this answer

































    0














    array[0] = p1++;


    Is the same as:



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


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






    share|improve this answer
























    • I think I'm aware of the meaning of using ++, still my soltions makes sense to me (I still didn't spot the issue). I've also tried debugging a in the part of filling the array with increments the array realy looked like it contains correct numbers. For example my random numbers were: 1, 4, 1, 2, 4 and the variables p1=2, p2=1, p3=0, p4=2, p5=0..

      – Robert Zelenka
      Nov 20 '18 at 23:05








    • 1





      Oooh wait. I think I see your point now! :-)

      – Robert Zelenka
      Nov 20 '18 at 23:11











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402729%2fjava-histogram-from-random-generated-numbers%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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("nnHistogram: ");

    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.





    share|improve this answer






























      0














      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("nnHistogram: ");

      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.





      share|improve this answer




























        0












        0








        0







        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("nnHistogram: ");

        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.





        share|improve this answer















        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("nnHistogram: ");

        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.






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 '18 at 23:05

























        answered Nov 20 '18 at 22:54









        Michael KrauseMichael Krause

        3,37111420




        3,37111420

























            0














            array[0] = p1++;


            Is the same as:



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


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






            share|improve this answer
























            • I think I'm aware of the meaning of using ++, still my soltions makes sense to me (I still didn't spot the issue). I've also tried debugging a in the part of filling the array with increments the array realy looked like it contains correct numbers. For example my random numbers were: 1, 4, 1, 2, 4 and the variables p1=2, p2=1, p3=0, p4=2, p5=0..

              – Robert Zelenka
              Nov 20 '18 at 23:05








            • 1





              Oooh wait. I think I see your point now! :-)

              – Robert Zelenka
              Nov 20 '18 at 23:11
















            0














            array[0] = p1++;


            Is the same as:



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


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






            share|improve this answer
























            • I think I'm aware of the meaning of using ++, still my soltions makes sense to me (I still didn't spot the issue). I've also tried debugging a in the part of filling the array with increments the array realy looked like it contains correct numbers. For example my random numbers were: 1, 4, 1, 2, 4 and the variables p1=2, p2=1, p3=0, p4=2, p5=0..

              – Robert Zelenka
              Nov 20 '18 at 23:05








            • 1





              Oooh wait. I think I see your point now! :-)

              – Robert Zelenka
              Nov 20 '18 at 23:11














            0












            0








            0







            array[0] = p1++;


            Is the same as:



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


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






            share|improve this answer













            array[0] = p1++;


            Is the same as:



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


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







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 22:53









            westonweston

            39.5k1696170




            39.5k1696170













            • I think I'm aware of the meaning of using ++, still my soltions makes sense to me (I still didn't spot the issue). I've also tried debugging a in the part of filling the array with increments the array realy looked like it contains correct numbers. For example my random numbers were: 1, 4, 1, 2, 4 and the variables p1=2, p2=1, p3=0, p4=2, p5=0..

              – Robert Zelenka
              Nov 20 '18 at 23:05








            • 1





              Oooh wait. I think I see your point now! :-)

              – Robert Zelenka
              Nov 20 '18 at 23:11



















            • I think I'm aware of the meaning of using ++, still my soltions makes sense to me (I still didn't spot the issue). I've also tried debugging a in the part of filling the array with increments the array realy looked like it contains correct numbers. For example my random numbers were: 1, 4, 1, 2, 4 and the variables p1=2, p2=1, p3=0, p4=2, p5=0..

              – Robert Zelenka
              Nov 20 '18 at 23:05








            • 1





              Oooh wait. I think I see your point now! :-)

              – Robert Zelenka
              Nov 20 '18 at 23:11

















            I think I'm aware of the meaning of using ++, still my soltions makes sense to me (I still didn't spot the issue). I've also tried debugging a in the part of filling the array with increments the array realy looked like it contains correct numbers. For example my random numbers were: 1, 4, 1, 2, 4 and the variables p1=2, p2=1, p3=0, p4=2, p5=0..

            – Robert Zelenka
            Nov 20 '18 at 23:05







            I think I'm aware of the meaning of using ++, still my soltions makes sense to me (I still didn't spot the issue). I've also tried debugging a in the part of filling the array with increments the array realy looked like it contains correct numbers. For example my random numbers were: 1, 4, 1, 2, 4 and the variables p1=2, p2=1, p3=0, p4=2, p5=0..

            – Robert Zelenka
            Nov 20 '18 at 23:05






            1




            1





            Oooh wait. I think I see your point now! :-)

            – Robert Zelenka
            Nov 20 '18 at 23:11





            Oooh wait. I think I see your point now! :-)

            – Robert Zelenka
            Nov 20 '18 at 23:11


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402729%2fjava-histogram-from-random-generated-numbers%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)