I am working on this lotto project which generates 6 random numbers in which are put into an array. How do i put it in an array where the numbers don't repeat?

Recommended Answers

All 18 Replies

Whenever you generate a new random number before you put it at the array check if the array already has it. If it does calculate it again, else put it and generate the next number.

I get a weird compile error, im still confused on how to put random numbers into an array

import java.util.Random;

public class Lotto
    {
        public static void main(String[] args)
        {
        int[] winningNum = new int[];//declares 6 winning lotto numbers


        Random randomNumbers = new Random(); //create a Random object

        winningNum = randomNumbers.nextInt(59);

Im getting a compile error. i still can't figure out how to put random numbers into an array.

import java.util.Random;

public class Lotto
    {
        public static void main(String[] args)
        {
        int[] winningNum = new int[6];//declares 6 winning lotto numbers


        Random randomNumbers = new Random(); //create a Random object

        winningNum = randomNumbers.nextInt(59);

winningNum is an array. You need to put the first number into winningNum[0], the next into winningNum[1], etc

so it will be liek this? i get an array OF

import java.util.Random;

public class Lotto
    {
        public static void main(String[] args)
        {
        int num1;
        int num2;
        int num3;
        int num4;
        int num5;
        int num6;

        Random randomNumbers = new Random(); //create a Random object

        num1=randomNumbers.nextInt(100);
        num2=randomNumbers.nextInt(100);
        num3=randomNumbers.nextInt(100);
        num4=randomNumbers.nextInt(100);
        num5=randomNumbers.nextInt(100);
        num6=randomNumbers.nextInt(100);

        int winningNum[]= new int[6];


                winningNum[] = num1;
                winningNum[] = num2;
                winningNum[] = num3;
                winningNum[] = num4;
                winningNum[] = num5;
                winningNum[] = num6;







        System.out.println("The winning numbers are " + winningNum);
        }
    }

Like this:

winningNum[0] = num1;
winningNum[1] = num2;
winningNum[2] = num3;
winningNum[3] = num4;
winningNum[4] = num5;
winningNum[5] = num6;

some reason i get a weird display output. its like not real numbers.

some reason i get a weird display output. its like not real numbers.

You can't print an array like that. Try looping through each element:

for (int i = 0; i < 6; i++)
{
    System.out.print (winningNum[i] + " ");
}

one other problem, how do i make so that no numbers repeat?

one other problem, how do i make so that no numbers repeat?

Already been answered:

Whenever you generate a new random number before you put it at the array check if the array already has it. If it does calculate it again, else put it and generate the next number.

so would this be correct?

if
    {
            num2 = num1;
            num2=randomNumbers.nextInt(59);
        {
            winningNum[1] = num2;
        }
    }

    else
    {
        winningNum[10 = num2;
    }

No, that does not work. What you need to do is each time you generate a random number, check every index of the array (using a for loop) to see if that random number has been generated before. If it has, generate a random number and check again. If it hasn't, put it into the array. James Cherill showed you how to use a for loop and put numbers into an array. Here is pseudocode for how you could go about filling an array with different random numbers.

int[] array = new int[10];
int number_filled = 0;
while(number_filled != array.length){
//Generate a random number
//using a for loop, check if the number has been used before
//if it has been used before, do nothing
//if it has not been used before, put it in array[number_filled] and increment number_filled by one. (Add one to it)
}

And you aren't going to get many different solutions (other than the one JavaAddict has given, and that I have basically repeated) because there aren't any. There might be methods in Java that do this for you, but any piece of code that is written to generate different random numbers will have to check the array to see if the number was already generated, or else somehow tell the number generator not to ever generate repeats.

can i just compare the numbers before i put them into the arrray?

What You are asking is written above:

//using a for loop, check if the number has been used before
//if it has been used before, do nothing
//if it has not been used before, put it in array[number_filled] and increment number_filled by one. (Add one to it)

In Java language:

do
{
      int num = new Random.nextInt(59);

      boolean isNotInArray = true;
      for( int i=0; i < numberOfElementsInArray; i++ )
      {
            if( num == myArray[i] )
            {
                 isNotInArray = false;
                 break;
            }        
      }
}
while( !isNotInArray );

myArray[numberOfElementsInArray++] = num;

Why would you give him the code. He will not learn anything from that.

I wouldn't use that code anyway, my professor would think that i was "cheating" lol thats y i want to use a more simple approach. by first generating the 6 numbers, compare them then putting them into an array.

now to display the numbers in ascending order. i tried using .length but for some reason it aint working

nt[] winningNum = new int[6];  //declares winningNum in an array

        for(int i=0;i<6;++i)
            {
                winningNum[i] = (int) (Math.random()*58+1); 

        for(int j=0;j<i;++j)
            {
                if(winningNum[i] == winningNum[j])
                           --i;
             }

             }
    System.out.println("The winning numbers are "); 

    for (int i = 0; i < winningNum.length; i++)
    {
        System.out.print(winningNum[i] + " ");  

    }

     System.out.println(""); 
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.