import java.util.Random;
public class DistinctDigitRandomNumbers {
/*
Generate a single random integer of length L whose digits are all distinct.
The function returns the number as a string to preserve leading digits if needed.
*/
public static String generateDistinctDigitNumber(int L, Random rng) {
// Digits 0–9 are available; we will pick L distinct digits.
// Using an array to store the chosen digits.
// The result string will be built step by step.
boolean[] used = new boolean[10];
StringBuilder result = new StringBuilder(L);
int count = 0;
while (count < L) {
char d = (char) ('0' + rng.nextInt(10));
// If digit not used yet, accept it.
if (!used[d - '0']) {
used[d - '0'] = true;
result.append(d);
count++;
}
}
return result.toString();
}
/*
Generate N random integers, each with distinct digits and length L.
Prints them directly.
*/
public static void generateNumbers(int N, int L, Random rng) {
for (int i = 0; i < N; i++) {
String s = generateDistinctDigitNumber(L, rng);
System.out.println(s);
}
}
public static void main(String[] args) {
int N = 10; // how many numbers to generate
int L = 6; // length of each number
// Random engine setup: seeded with current time.
Random rng = new Random();
// Generate the numbers
System.out.println("Generated " + N + " numbers of length " + L + " with distinct digits:");
generateNumbers(N, L, rng);
}
}
/*
run:
Generated 10 numbers of length 6 with distinct digits:
297810
179265
073918
865931
238057
136479
564973
517396
718423
973064
*/