Skip to content

Instantly share code, notes, and snippets.

@SergKolo
Created September 15, 2015 17:04
Show Gist options
  • Save SergKolo/345d19f713d9e7838f27 to your computer and use it in GitHub Desktop.
Save SergKolo/345d19f713d9e7838f27 to your computer and use it in GitHub Desktop.
Lottery. Generate 6 random numbers untill they match your guess. Do this until you hit jackpot. Once you hit jackpot , repeat that 25 times.
import java.lang.Math;
import java.util.Random;
import java.util.Arrays;
public class scratch
{
public static void main ( String [] args )
{
int lottoNums[] = new int [6];
int myNums[] = {1,13,15,22,41,36};
int countMatched = 0;
/* for (int i = 0; i < 6; i++)
{
lottoNums[i] = getRandomInt(1,41);
}
System.out.println(lottoNums.length);
for (int i = 0; i < 6; i++)
{
System.out.print(lottoNums[i] + "#");
}
*/
for (int x = 1; x<= 25; x++)
{
System.out.println("Game #" + x);
while (true)
{
//System.out.println("generating numbers");
for (int i = 0; i < 6; i++)
{
lottoNums[i] = getRandomInt(1,41);
}
if (hasDuplicates(lottoNums))
{
//System.out.println("Duplicates found");
continue;
}
/*
Arrays.sort(lottoNums);
for(int i = 1; i < numbers.length; i++) {
if(numbers[i] == numbers[i - 1]) {
continue;
}
}
*/
///////////////////////////////////////////
/* for (int i = 0; i < 6; i++)
{
System.out.print(lottoNums[i] + "#");
}
System.out.printf ("\n");
*/
//compare myNums to lottoNums
// count matched numbers
//System.out.println("Comparing");
for (int i = 0; i < 6; i++)
{
for (int j = 0 ; j < 6; j++)
{
if (lottoNums[j] == myNums[i])
countMatched++;
}
}
// if we matched 6 numbers, we've got a jackpot
if ( countMatched == 6 )
{
System.out.println("Jackpot");
break;
}
/*else if (countMatched == 3)
{ System.out.println("Matched 3");} */
//we didn't match 6, so reset the counter
countMatched = 0;
}
}
}
public static int getRandomInt (int low, int high)
{
int out;
//out = low + (int)(Math.random()*((high-low) + 1 ));
Random rand = new Random();
out = rand.nextInt((high - low) + 1) + low;
// if (out == 0 || out == 42)
// System.out.print("ERROR" + out);
return out;
//System.out.print(out + "\n");
}
public static boolean hasDuplicates (int[] a)
{
boolean result = false;
Arrays.sort(a);
for(int i = 1; i < a.length; i++)
{
if(a[i] == a[i - 1])
{
result = true;
}
}
return result;
}
}
@ByteCommander
Copy link

Optimizations:

  • int getRandomInt(int low, int high): Make the Random object static and don't create a new instance of it every time!
  • Don't use the hasDuplicates function. Use a LinkedHashSet instead of an array to store the numbers. That will not store duplicate items. Just keep filling numbers into it until its size is big enough.
  • This is probably not faster but better style: Don't compare every lotto number with every guessed number, I would sort both lists instead e.g. with Arrays.sort(int[] a) and compare each corresponding item only.

@aboettger
Copy link

  • Class names starts with an uppercase character Scratch instead of scratch
  • import java.lang.Math; is never used
  • Change the visibility (private) for getRandomInt and hasDuplicates. No need for public

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment