I'm trying to use an array to print out 6 numbers out of 42 since it's a lotto code
Here's the question the lecturer wants me to do:
Write a Java program to achieve the following:
 Declare an array of type int of size 6
 Randomly generate your lotto numbers for next week and store them
in the array using a loop…good luck!
 Print the array to the screen

import java.util.Random;
public class W2P8{
    public static void main(String[]args){
    int[] randomNumbers = new int[6];
    Random rn = new Random();

    for (int i = 0; i < randomNumbers.length; i++)
        {
            int randomInt = rn.nextInt(42) + 1;
            System.out.println(randomInt);
        }//end of for loop
    }//end of main
}//end of class

This prints out 6 random numbers but prints out duplicates.

Recommended Answers

All 3 Replies

I think following is might be useful to you.
Here you just have to store random no. into score array.
After storing 1st element you have to check it with other random value.

for(int x=0;x<=5;x++)
      {
          score[x]=(int)(1+Math.random()*6);
          if(x>0)
              for(int c=0;c<=x-1;c++)
                  if(score[c]==score[x])
                      x--;
      }
for(int a = 0;a<score.length;a++)
{System.out.println(score[a]);}

You can generate any no. of random number by just changing into outer for loop.

suggestions:

before storing a random number into the given array we have to consider 2 things

  1. whether array contains desired number of random numbers or not (to print the final array elements)

  2. whether the generated random number is existed in the array previously or not

if we achieve those 2 things then we will get the result as you expected

the following code does 2 things which i have explained above

import java.util.Random;

public class RandomizedArrayExample
{
    public static int[] myNumbers = null;    
    public static void main(String[] args)
    {
      try
      {
          myNumbers = new int[6];
          System.out.println(" my initial values ");
          for (int i = 0; i < myNumbers.length; i++) {
               System.out.println(myNumbers[i]);
          }

          Random r = new Random();
          int total_elements_cnt = 0;
          boolean loop_status = true;

          while(loop_status)
          {
              int next_num = r.nextInt(42)+1;          
              if(!isCompleted()){
                  if(!isDuplicate(next_num)){
                      myNumbers[total_elements_cnt] = next_num;
                      total_elements_cnt++;
                  }else{
                      continue;
                  }
              }else{
                  loop_status = false;
              }
          }
          System.out.println("-------- my final values -------------");
          for (int i = 0; i < myNumbers.length; i++) {
               System.out.println(myNumbers[i]);
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
  }

  public static boolean isCompleted(){
      boolean status = true;
      for (int i = 0; i < myNumbers.length; i++){
          if(myNumbers[i]==0){
              status = false;
              break;
          }
      }
      return  status;
  }

  public static boolean isDuplicate(int num){
      boolean status = false;
      for (int i = 0; i < myNumbers.length; i++){
          if(myNumbers[i]== num){
              status = true;
              break;
          }
      }
      return  status;
  }
}

check it once and let me know the status

happy coding

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.