/*
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.
*/
function generateDistinctDigitNumber($L) {
// 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.
$used = array_fill(0, 10, false);
$result = "";
$count = 0;
while ($count < $L) {
// Always generate digits '0'..'9' reliably
$d = chr(48 + random_int(0, 9));
// If digit not used yet, accept it.
if (!$used[ord($d) - ord('0')]) {
$used[ord($d) - ord('0')] = true;
$result .= $d;
$count++;
}
}
return $result;
}
/*
Generate N random integers, each with distinct digits and length L.
Prints them directly.
*/
function generateNumbers($N, $L) {
for ($i = 0; $i < $N; $i++) {
$s = generateDistinctDigitNumber($L);
echo $s . "\n";
}
}
$N = 10; // how many numbers to generate
$L = 6; // length of each number
// Random engine setup: PHP's random_int is already cryptographically secure.
// Generate the numbers
echo "Generated $N numbers of length $L with distinct digits:\n";
generateNumbers($N, $L);
/*
run:
Generated 10 numbers of length 6 with distinct digits:
014752
893260
786951
736804
341678
713450
802963
067489
364701
304698
*/