Below code will generate random number for lotteries in Mega Millions or Power Ball format. Mega Millions and Power Ball are taken as examples only in this tutorial.
In Part 2 of this tutorial, we have discussed about different approach. Click here to go to part 2
package pkg_ABC;
import java.io.File;
import java.io.FileInputStream;
import java.util.Random;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Lucky_Numbers {
public static void main(String[] args) throws Exception {
// Mega Millions have two set of numbers from which you have to pick your numbers. You have to pick first 5 numbers from the set 1 to 70. And the Mega Ball number from 1 to 25.
// Mega Millions have two set of numbers from which you have to pick your numbers. You have to pick first 5 numbers from the set 1 to 70. And the Mega Ball number from 1 to 25.
int[] goodNumber = new int[5];
Random winningNumber = new Random();
// For the first 5 random numbers
for(int i=0; i < 5; i++)
{
// For the first 5 random numbers
for(int i=0; i < 5; i++)
{
int first_5_Numbers = winningNumber.nextInt(71); //This will generate random numbers in the range 0 to 70. To generate in power ball format, use nextInt(70) instead of nextInt(71).
while(first_5_Numbers == 0) {
first_5_Numbers = winningNumber.nextInt(71);
}
goodNumber[i] = first_5_Numbers;
//The random numbers will be stored in below variables.
//goodNumber[0] 1st lucky number
//goodNumber[1] 2nd lucky number
//goodNumber[2] 3rd lucky number
//goodNumber[3] 4th lucky number
//goodNumber[4] 5th lucky number
System.out.println("Good Number_"+i+" =
"+goodNumber[i]);
}
// For 6th number, this will be Mega Ball number if you compare this with Mega Millions.
int sixthNumber= winningNumber.nextInt(26); //This will generate random numbers in the range 0 to 25. To generate in power ball format, use nextInt(27) instead of nextInt(26).
while(sixthNumber == 0) {
sixthNumber= winningNumber.nextInt(26);
}
System.out.println("The 6th number = "+sixthNumber);
//Now the
lucky number is :
System.out.println("The lucky number = "+goodNumber[0]+" "+goodNumber[1]+" "+goodNumber[2]+" "+goodNumber[3]+" "+goodNumber[4]+" "+sixthNumber );
}
}
OUTPUT:
Good Number_0 = 49
Good Number_1 = 13
Good Number_2 = 60
Good Number_3 = 21
Good Number_4 = 14
The 6th number = 13
The lucky number = 49 13 60 21 14 13
No comments:
Post a Comment