Reputation: 1
import java.util.Random;
import java.util.Scanner;
class Lottery {
private int lotteryNumbers[];
public int [] lotteryPicks() {
lotteryNumbers = new int[499];
for (int i = 0; i < lotteryNumbers.length; i++) {
lotteryNumbers[i] = (int) (Math.random() * 55) + 1;
}
return lotteryNumbers;
}
This program (not all methods shown) takes the users input of 6 numbers ( 5 numbers and 1 megaball) and compares them with 6 random numbers generated by the computer to see how likely someone is to win the lottery. My problem is with the lotteryPicks() method. Here's the issue I don't know how to create 6 random numbers n amount of times( say 500 )? So ideally it would generate 6 numbers say 12,14,45,32,25,17 and the next time generate 6 new numbers until there's 500 sets of numbers. Also, can you return in a for-loop? Any help would be appreciated!
Upvotes: 0
Views: 3636
Reputation: 104050
Your simple (Math.random() * 55) + 1
might return 42, 42, 42, 42, 42, 42
. Lotteries typically do not allow repetition of the number. (One number per ball, one ball per number; once the ball falls out of the tumbler, that number cannot recur.) You should instead generate an array of your 55 numbers, randomly reorder the array, and then select the first six elements from the array.
There are two common shuffling algorithms, one that simply decorates each number with a random number, sorts on that random number, and then removes the random number after you're done (a specific instance of a trick known as a Schwartzian Transform in Perl circles, Decorate-Sort-Undecorate in Python Circles, sort_by
in Ruby circles, etc.). The other algorithm is an excellent choice, but an inexperienced programmer might make subtle but very huge mistakes in implementing it.
Also, can you return in a for-loop?
Yes, you can, but that is often the indication that the wrong type of loop was used. :)
Sometimes it makes the most sense to return from the middle of a for
loop, but frequently it makes more sense to re-write to a while() { }
loop or do { } while ()
loop instead. When in doubt, try writing both variants and pick whichever one reads the most clearly.
Upvotes: 1
Reputation: 6597
So yo could do what Bala R suggest and return a List<List<int>>
, where the outer lists holds all 600 "tickets", and the inner list holds the numbers in the ticket.
However, from an object oriented perspective, it would be better to create a Ticket class that contains the 6 numbers, there are many ways to do this -- here's my (pseudo - untested) code:
public class Ticket
{
private int[] numbers;
public Ticket()
{
numbers = new int[6];
for(int i=0; i < numbers.length; i++)
{
numbers[i] = (int) (Math.random() * 55) + 1;
}
}
// Provide some public methods for comparing the ticket and what not
}
Then in your main program - you can create a List of 600 (or so) Tickets, ex:
public ArrayList<Ticket> generateTickets
{
ArrayList<Ticket> retVal = new ArrayList<Ticket>();
for(int i=0; i < TICKETS_TO_GENERATE; i++)
{
retVal.add(new Ticket());
}
return retVal;
}
Then when that is done, you use your public methods on the Ticket class to do comparisons/etc.
And to answer your question, you can put a return statement inside a for, it's quite common (ie. when searching an array.)
-- Dan
Upvotes: 0
Reputation: 38531
You can declare lotteryNumbers
as a two dimension array with the first dimension indicating which set of numbers you're on, and the second dimension is each one of the six numbers.
private int lotteryNumbers[][] = new int[500][6];
Yes, you can return inside of a for loop. Also, you should declare a Random
variable and invoke nextInt(..)
instead of the approach you're using now which requires you to cast your result.
Upvotes: 1