-2

I want to generate 6 different random numbers by using Math.random and store all of them into an array. How can I make sure that they are different? I only have this so far and this has a bug. I only need numbers between 1 and 49. ( 1 + (int) (Math.random() * 49) ).

public static void main (String []args) {
    int []randomNumArray = new int [6];
    randomNumArray[0] = randomNumber();
    System.out.println(randomNumArray[0]);

    for (int i = 1; i < 6; i++) {                   
        for (int j = 0; j < i; j++) {              
            randomNumArray[i] = randomNumber();
            do {
                if (randomNumArray[i] == randomNumArray[j]) {
                    randomNumArray[i] = randomNumber();
                }
            } while(randomNumArray[i] == randomNumArray[j]);
        }
        System.out.println(randomNumArray[i]);
    }
}

//This method is for generating random numbers
public static int randomNumber (){
    return  ( 1 + (int) (Math.random() * 49) );
}
4
  • 1
    You mentioned that your code has a bug. How exactly does it manifest itself? Commented Mar 24, 2014 at 3:37
  • shuffling an array is a good way to get non-repeating elements. Why don't you want to use that? Commented Mar 24, 2014 at 3:37
  • When I run this code, the array still has duplication @merlin2011 Commented Mar 24, 2014 at 3:45
  • Should have just edited this question instead of starting a new one: stackoverflow.com/questions/22584244/… Commented Mar 24, 2014 at 3:49

2 Answers 2

3

Generate random numbers and keep adding them to a Set until its size = 6. A set can contain only unique elements. So, you are assured uniqueness.

EDIT :

public static void main(String[] args)  {
        Set<Integer> intSet = new LinkedHashSet<Integer>();
        Random r = new Random();
        while (intSet.size() <= 6) {
            intSet.add(r.nextInt(49)); // or your method of generating random numbers
        }
        System.out.println(intSet);
    }
Sign up to request clarification or add additional context in comments.

7 Comments

This works especially well if the number you want (6) is much smaller than the possible options (49). If they are close, it will perform poorly as it becomes harder to draw "new numbers".
@Thilo - Yes. I agree. Is there an efficient way to assure/enforce uniqueness?
shuffling the array is the other good approach.
It depends on performance requirements. Even generating 48 unique numbers between 1 and 49 takes a negligible amount of time for many applications. By the way, I suggest a LinkedHashSet if you want to maintain a nice randomly generated order.
@WhoAmI Can you show me how? I kinda don't get it...
|
0

Obviously, the set approach and shuffling the array are more efficient approaches, but given the context, here is a modification of the OP's code to achieve uniqueness, to address the "bug" in his code.

import java.util.*;

public class Main {
    public static void main (String []args) {

        int []randomNumArray = new int [6];

        randomNumArray[0] = randomNumber();

        System.out.println(randomNumArray[0]);


        for (int i = 1; i < 6; i++)
        {                   
            int candidate;
            boolean foundMatch;
            do
            {
                candidate = randomNumber();
                foundMatch = false;
                for (int j = 0; j < i; j++)
                    if (candidate == randomNumArray[j])
                        foundMatch = true;
            } while (foundMatch);
            randomNumArray[i] = candidate;

            System.out.println(randomNumArray[i]);
        }
    }
    //This method is for generating random numbers
    public static int randomNumber (){
        return  ( 1 + (int) (Math.random() * 49) );
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.