r/learnjava • • Jun 06 '21

How should I write a program to generate lottery numbers?

I want to write a program to generate lottery numbers. For brute forcing the odds and playing around like that.

I would like to give it a bit of versatility, being able to specify number of balls drawn, from what sized pool, How many supplementary numbers, and also if there is a bonus ball or two (Sometimes called power ball, has its own set of balls).

I know how to generate random numbers but there will be collisions (same number potentially drawn twice) which doesn't happen in lottery of course. I'm wondering if I should just generate another random number if a collision is detected, or I should use an Array of some kind.

Any suggestions?

48 Upvotes

18 comments sorted by

43

u/desrtfx Jun 06 '21

I know how to generate random numbers but there will be collisions

That's why one does not do it that way.

The easier and collision free way is to generate an array of all the numbers in the lottery pool, shuffle the array, and pick the first "x" numbers.

5

u/[deleted] Jun 06 '21

thank you for the advice friend. which array did you recommend and how do I shuffle it.

14

u/desrtfx Jun 06 '21

A normal array and your own shuffle, see Fisher-Yates Shuffle.

Or, an ArrayList and the shuffle built into the Collections framework.

1

u/JustCallMeFrij Jun 07 '21

Personally I'm more of a boogy-woogy man myself for randomizing array contents.

6

u/[deleted] Jun 06 '21

Which, incidentally, is how actual lotteries with physical balls basically work.

9

u/nutrecht Jun 06 '21

Here: https://docs.oracle.com/javase/8/docs/api/java/util/Random.html

That, a few loops and asking the user for input is pretty much all you need.

1

u/doodooz7 Jun 07 '21

How about finding a list of past winning numbers and add them to a database. Then you can do all kinds of fun queries.

1

u/[deleted] Jun 07 '21

I have an interesting project in mind. I'll be sure to let you know when I win. its fascinating how different the odds are. its easy to calculate them say for example you need 6 numbers drawn from 45 its 1/(6/45)(5/44)(4/43)(3/42)(2/41)*(1/40) and when it needs a bonus ball and higher ball count it gets pretty crazy.

6

u/Caracalla81 Jun 06 '21

The other commenters have answered your question. I just want to add that the numbers you draw won't be truly random - computers can't do true randomness. That's why lotteries use those ball machines.

8

u/steave435 Jun 06 '21

They're not truly random either. Which ball comes out is determined by the position of the balls when the opening or grabber or whatever mechanism they use is activated. That's determined by their starting positions and time elapsed etc.

As long as you can't predict the results it doesn't really matter.

1

u/FleshKnot Jun 06 '21

But you have to make sure it's an unbiased result. Sure, we don't know the result but if there's a higher chance of a specific number being drawn or if there's a pattern; it would be flawed and people would catch on to the pattern.

1

u/steave435 Jun 06 '21

The real issue would be that without checking the source code, there's no way for participants to verify that you're not just telling it what to output. Whether there's some slight bias towards a certain number or not isn't really that important, especially since this is obviously not going to be for running a real world big lottery.

It's a programming challenge. There's no need to complicate it with the other stuff.

1

u/helicoid Jun 06 '21

Casinos use computers all the time. You can play roulette on a touch screen instead of having an actual guy spin the ball around the wheel.

1

u/bocephus205 Jun 06 '21

I'm a noob, but here is how I did it

-create ArrayList

-add random number (between 1-pool size) if it's not already in the List, repeat by number of balls specified

- return list

- pick random number (between 1-powerball size)

-return powerball number

not sure what you mean by supplementary numbers

3

u/desrtfx Jun 06 '21

While your approach is working (and not ultimately wrong), you are skewing the results of the random number generator.

The general approach is to create an array that holds all the numbers (just like the lottery balls in reality) and then shuffle the array and draw an arbitrary set of the size that should be the winning numbers. This can be the first x in sequence, the last x in sequence, any random numbers, but there you need to remove the drawn numbers).

By using the array approach and shuffling, repeated numbers of the random number generator don't affect the result. Sufficient shuffling will always result in a decent (not perfect) random distribution.

2

u/steave435 Jun 06 '21

While I'm sure that your method is more efficient, I don't understand how his method would skew the results. Assuming the random number generator is truly random (I know it's not, but an imperfect generator would skew the results no matter what method is used), each number should have the same chance of being drawn, even if it has to go trough a few re-draws due to drawing old numbers. How's that skewing the results?

1

u/bocephus205 Jun 06 '21
    //Shuffle method

Number of balls?
5
Pool size?
15
mega ball size
25
unshuffled
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
shuffled
[13, 14, 9, 3, 11, 7, 8, 12, 6, 2, 1, 4, 10, 15, 5]
 
Result:
13, 14, 9, 3, 11, MegaBall 19

    
//What I tried first

Number of balls to be drawn?
5
Pool size - must be larger than number of balls drawn
15
Mega Ball size? 
25
Your numbers
[13, 14, 4, 1, 10]
Megaball = 16

I'm still learning, but having a hard time visualizing the difference between the two