3

I am having an assignment about creating password through Java:

Suppose you work in a safe selling company and your manager asked you to create a list of all the ten digit numbers between 0000000000 and 9999999999 without repeating a digit in the same number. What is the method to do this algorithm in JAVA?

Here's what I've done so far:

public static long generateNumber()  
{  
    String s1 = "33333";  
    double d = Math.random();  
    d=d*100000.0;  
    int i = (int) d;  
    String s2 = String.valueOf(i);  
    String s3=s1+s2;  
    long m = Long.parseLong(s3);  
    return m;
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Omar Mustafa
  • 31
  • 1
  • 1
  • 2
  • 7
    I'd probably consider shuffling a collection of the digits. – Dave Newton May 04 '13 at 15:16
  • Your program always gives you a number, that starts with 33333, so it repeats the digit 3, apart from that, it gives you only a single number, not all numbers – Ingo May 04 '13 at 15:33

3 Answers3

4

If you're looking for ten digit numbers without any duplicate digits, you're effectively looking to generate all permutations of all digits, i.e. the string "0123456789".

There are other threads on SO to help you with this, for example these

Community
  • 1
  • 1
MisterS
  • 65
  • 5
  • no i don't want permutations i am asked to generate all the 10 digits numbers between 0000000000 & 9999999999 using array & random method – Omar Mustafa May 04 '13 at 17:49
  • @OmarMustafa This request is bullshit, there is no guarantee that a random method generates **all** elements. – Ingo May 04 '13 at 19:39
  • Omar, you stated that you wanted all numbers but `without repeating a digit in the same number`! Nothing about arrays, and nothing about "random method". Approaches using random elements is useless in this case, as it's never guaranteed to produce sufficient diverse input - a random number generator could (in theory) spit out only the number 2 forever... If you want 10-digit numbers without any repeating digits, well: that's the same thing as all permutations of the string "0123456789" as I posted above. – MisterS May 07 '13 at 19:31
  • Generating all possible strings is not the same as generating all possible combinations of 10 digits without repetition. Strings may contain non-numeric characters. – Anderson Green May 22 '13 at 05:37
1

A simplstic way that uses little code is:

List<Long> combos = new ArrayList<>();
Set<Character> chars = new HashSet<>();
for (long i = 1000000000; i < 9999999999L; i++) {
    chars.clear();
    for (char c : String.valueOf(i).toCharArray()) {
        chars.add(c);
    }
    if ((chars).size() == 10) {
        combos.add(i);
    }
}

Not very effeicient, but does the job.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

Using a shuffling technique:

    public static void main(String[] args) {
        List<Integer> passwords = Arrays.asList( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 );
        for (int i = 0; i < 10; i ++)
        {
            Collections.shuffle(passwords);
            String p = toFlatString(passwords);
            System.out.println(p);
        }
    }

    private static String toFlatString(List<Integer> list) {
        StringBuilder sb = new StringBuilder();
        for (int i : list)
            sb.append(i);
        return sb.toString();
    }

Output: 2651803497 2936745018 7064918235 1594670823 4035872619 6432971850 6387925401 7103649285 9712380645 9321574806

Jeremy Unruh
  • 649
  • 5
  • 10
  • this way is good but i want a list of all the numbers that can be generated – Omar Mustafa May 06 '13 at 09:17
  • This doesn't answer the question, since the question is asking how to generate all possible permutations. This implemention only generates a handful of the possible permutations. – Anderson Green May 22 '13 at 05:35