Buzz Code
Published in

Buzz Code

Java 8 | Array & Random Practice — Lottery 2

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!

--

--

--

Happy coding!

Recommended from Medium

what i have been waiting for has come !

The Apprentice’s Library

CrowdRiff’s CTO draws on personal experience to support and nurture talent

How to enable or disable the “Auto-hide” feature of the Taskbar using desktop shortcuts

Analyzing issues caused by OOM Killer

Java Application Optimization on Kubernetes on the Example of a Spring Boot Microservice

How to get 50 $ in digitalocean free github student develpoer pack

How to install WordPerfect 7 for Windows 95 on Windows XP (non NT version)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Student Kim

Student Kim

Learn something new everyday.

More from Medium

Java Streams for Summing Numbers

Best Practices for Creating Java Object Classes

Useless Java: Day 3

COLLECTIONS IN JAVA