Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a 4 digit Random Number using java with no repetition in digits

I wrote a code using java to create a random 4 digit number with no repetition of digits, the code I wrote is given below :-

Random r = new Random();
d1 = r.nextInt(9);
d2 = r.nextInt(9);
d3 = r.nextInt(9);
d4 = r.nextInt(9);
while(d1==d2||d1==d3||d1==d4||d2==d3||d2==d4||d3==d4)
{
    if(d1==d2||d2==d3||d2==d4)
    {
        d2 = r.nextInt(9);
    }
    if(d1==d3||d2==d3||d3==d4)
    {
        d3 = r.nextInt(9);
    }
    if(d1==d4||d2==d4||d3==d4)
    {
        d4 = r.nextInt(9);
    }
}   
System.out.println(d1+""+d2+""+d3+""+d4);


here are the test cases(generated from System.out.println(R1+""+R2+""+R3+""+R4);) are as following :-

 0123 |  OK as required
 1234 |  OK as required
 2123 |  not OK because 2 is present more than one time 
 9870 |  OK as required
 0444 |  not OK because 4 is present more than one time


Now My question here is, that if there is some better way to do this. If I could enhance it in some way?

like image 747
kakabali Avatar asked Aug 19 '13 15:08

kakabali


4 Answers

A couple of approaches:

  1. Use a Set to hold the digits and keep adding random digits until the set has four values in it.

  2. Create an array with values 0-9 in it. Shuffle the array and take the first four values.

If performance matters, you will want to try a couple of different methods and see which is faster.

like image 20
Rob Avatar answered Sep 23 '22 18:09

Rob


Create a list of integers from 0 to 9, shuffle it and extract the first 4.

public static void main(String[] args) {
    List<Integer> numbers = new ArrayList<>();
    for(int i = 0; i < 10; i++){
        numbers.add(i);
    }

    Collections.shuffle(numbers);

    String result = "";
    for(int i = 0; i < 4; i++){
        result += numbers.get(i).toString();
    }
    System.out.println(result);
}

There's some ugly string-to-int conversing going on, but you get the idea. Depending on your use case you can see what is needed.

like image 157
Jeroen Vannevel Avatar answered Sep 23 '22 18:09

Jeroen Vannevel


Here's my approach, even though it uses a lot of string parsing but no data structure:

 static int generateNumber(int length){
            String result = "";
            int random;
            while(true){
                random  = (int) ((Math.random() * (10 )));
                if(result.length() == 0 && random == 0){//when parsed this insures that the number doesn't start with 0
                    random+=1;
                    result+=random;
                }
                else if(!result.contains(Integer.toString(random))){//if my result doesn't contain the new generated digit then I add it to the result
                    result+=Integer.toString(random);
                }
                if(result.length()>=length){//when i reach the number of digits desired i break out of the loop and return the final result
                    break;
                }
            }

            return Integer.parseInt(result);
        }
like image 33
MZ4Code Avatar answered Sep 21 '22 18:09

MZ4Code


Use a Set maybe?

Random r = new Random();
Set<Integer> s = new HashSet<Integer>();
while (s.size() < 4) {
    s.add(r.nextInt(9));
}
like image 20
fred02138 Avatar answered Sep 24 '22 18:09

fred02138