Java 8 | Array & Random Practice — Lottery 2

Student Kim
Buzz Code
Published in
2 min readJan 5, 2021

Hello! Last time we made a lotto number generator, today let’s make an improved lotto program! If you missed the last post, checkout the link below.

[Question] Generate the winning lottery program by following rules.

  1. From 1 to 52, pick the random numbers 1000 times.
  2. pick 5 numbers that was picked the most.

I’ll give you a big hint here. This time let’s use the index as the number 1 to 52, and use the for statement pick the random numbers 1000 times and when every time the random number coming out then add 1 on the index that is same with the random number!

Just like the picture above! Let’s do this now, you can do this!

So I used the for statement to pick the random numbers 1000 times, and I put increment happyBall[rNum]++ , so we can see how many times each number got picked.
And I declared the variable most and if happyBall[most] is smaller than the happyBall[b] which is check through all the indexes in the array, put the b in the most. So the most becomes the number that got picked the most among 1 to 52, and happyBall[most] becomes the number that how many times it got picked. And for the last, I changed the value of the happyBall[most] into -1, So it can’t be picked more than once!

I guess that’s all for today guys! Now you all should go to get a lottery tickets with the numbers you generated today! haha Thank you for reading my post, and happy new year!

--

--