-2

I need help writing a method that will return a random 4 digit number with no repetitions. I am not allowed to use string in any way...

This is what i have so far and the line where ran. is involved, im getting an error: ran cannot be resolved

public static int generateSecretNumber() {
    Random r = new Random();
    int x = r.nextInt(1000);;    
    x = x + 1000;  
    return x;
Cœur
  • 37,241
  • 25
  • 195
  • 267

5 Answers5

2

You can generate 4 independent numbers.

1) generate number A between 1 and 9

2) generate number B between 0 and 9 and different from A

3) generate number C between 0 and 9 and different from A and B

4) generate number D between 0 and 9 and different from A, B and C

Now your number is ABCD or

1000*A + 100*B + 10*C + D

Full code:

public static int generateSecretNumber() {
    Random ran = new Random();
    int a, b, c, d;

    a = ran.nextInt(9) + 1; //1-9

    do {
        b = ran.nextInt(10); //0-9
    } while(b==a);

    do {
        c = ran.nextInt(10); //0-9
    } while(c==a || c==b);

    do {
        d = ran.nextInt(10); //0-9
    } while(d==c || d==b || d==a);

    return 1000*a + 100*b + 10*c + d;
}
kajacx
  • 12,361
  • 5
  • 43
  • 70
  • Im still getting "ran cannot be resolved" and how do i make it that the numbers generated are all distinct? – user2204808 Mar 26 '13 at 23:27
  • Would this be considered as using string though? if not, how can i implement it into my method so that i can call it back later? – user2204808 Mar 26 '13 at 23:32
  • you can convert number to string by (number+"") - simply add empty string – kajacx Mar 26 '13 at 23:33
  • Im not allowed to use any string at all – user2204808 Mar 26 '13 at 23:36
  • Well, this doesnt use any strings at all. Ill write the whole method with header and return for you. – kajacx Mar 26 '13 at 23:39
  • This will work and is very helpful, but I don't like the idea of having a do-loop per digit. What if you wanted to change the problem to do the same, but for 10 digits? Are you going to just keep adding do-loops? Not the most flexible approach you could have. – 2to1mux Mar 26 '13 at 23:52
  • You can add an array of boolean values, witch will indicate weather the number was used or not, or you can have a List of used numbers. Then you can generate your numbers in a cycle. – kajacx Mar 27 '13 at 16:18
2

The line:

int x = + ran.nextInt(1000);

should read

int x = r.nextInt(1000);

Another thing - you say you want to generate a random 4 digit number with no repetitions. This could take a while as it is perfectly ok for a random number generator to return the same number multiple times, in the same way that when you flip a coin you can get 4 heads in a row.

ngsmrk
  • 36
  • 2
1

Generate 4 random digits between 0 and 9 (argument to nextInt: 10). Keep track of all 4 digits. If any of them are the same, then generate another random digit. Then construct your final number using the digits.

Additionally, if you are going to declare your Random variable r, then use r.nextInt(10) not ran.nextInt(10).

rgettman
  • 176,041
  • 30
  • 275
  • 357
1

For a 4 digit random number with distinct digits you could just shuffle a collection.

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
do{
    Collections.shuffle(numbers);
} while (0 == numbers.get(0));

System.out.println(numbers.subList(0, 4));
samlewis
  • 3,950
  • 27
  • 27
  • This is a very unique idea and I like it. It wouldn't work if you needed a number longer than 10 digits, however, I suppose the same is true of using a 32-bit integer. +1 for creativity – 2to1mux Mar 27 '13 at 00:28
0

There are a lot of different ways you could do this to get distinct numbers. I advise something like the following:

public static int generateSecretNumber(int digitLength) {
    Random ran = new Random();
    int randDigit[digitLength];
    int finalNum = 0;
    for(int i = 0; i < digitLength; i++){
        randDigit[i] = ran.nextInt(10);
        int j = 0;
        while(j < i){
            if(randDigit[j] == randDigit[i]){
                randDigit[i] = ran.nextInt(10);
                j = 0;
                continue;
            }
            j++;
        }

    }

    for(int i = 0; i < digitLength; i++){
        finalNum = finalNum + (randDigit[i] * Math.pow(10, i));
    }

    return finalNum;
}
2to1mux
  • 773
  • 5
  • 7
  • i get "cannot be resolved as a variable" for randDigit. how would i fi this?? – user2204808 Mar 27 '13 at 00:10
  • Sorry, I had a typo in the declaration of the array. Will edit original right now, but the array declaration "int randDigits[digitLength];" should be "int randDigit[digitLength];" – 2to1mux Mar 27 '13 at 00:24