[Solved] Generate random number of 6 digit length with at-least n unique digits in php


Asker Comment: 4 unique and 2 repetitive digits. But they should also have 3 unique
and 3 repetitive digits, 2 unique and 4 repetitive digits in random
number.

Ok, I think I finally understand, so I have tweaked the algorithm to match every possible rule you’re expecting from 0 to 6 unique, though as you will see ive not changed much to my original answers code. Ive even added a sort flag so as to sort the int’s incase that is also a requirement. Im determined!!

<?php
//
function random_number_with_dupe($len = 6, $dup = 1, $sort = false) {
    if ($dup < 1) {
        throw new InvalidArgumentException('Second argument is < 1');
    }        

    $num = range(0,9);
    shuffle($num);

    $num = array_slice($num, 0, ($len-$dup)+1);

    if ($dup > 0) {
        $k = array_rand($num, 1);
        for ($i=0; $i<($dup-1); $i++) {
            $num[] = $num[$k];
        }
    }

    if ($sort) {
        sort($num);
    }

    return implode('', $num);
}

All unique.

for ($i=0; $i<5; $i++) {
    echo random_number_with_dupe(6, 1, true).PHP_EOL;
}
/*
124579
123679
013568
015789
013578
*/

4 unique and 2 repetitive digits.

for ($i=0; $i<5; $i++) {
    echo random_number_with_dupe(6, 2, true).PHP_EOL;
}
/*
235699
037789
034677
012249
033569
*/

3 unique and 3 repetitive digits.

for ($i=0; $i<5; $i++) {
    echo random_number_with_dupe(6, 3, true).PHP_EOL;
}
/*
015559
011147
111239
677789
456777
*/

2 unique and 4 repetitive digits.

for ($i=0; $i<5; $i++) {
    echo random_number_with_dupe(6, 4, true).PHP_EOL;
}
/*
068888
022229
018888
333378
000058
*/

1 unique and 5 repetitive digits.

for ($i=0; $i<5; $i++) {
    echo random_number_with_dupe(6, 5, true).PHP_EOL;
}
/*
788888
066666
799999
355555
244444
*/

0 unique and 6 repetitive digits.

for ($i=0; $i<5; $i++) {
    echo random_number_with_dupe(6, 6, true).PHP_EOL;
}
/*
888888
333333
777777
888888
777777
*/

All rules.

for ($i=0; $i<6; $i++) {
    echo random_number_with_dupe(6, $i+1, true).PHP_EOL;
}
/*
345789
003569
245666
000089
777778
555555
*/

All rules (random).

for ($i=0; $i<6; $i++) {
    echo random_number_with_dupe(6, mt_rand(1, 6), true).PHP_EOL;
}
/*
225678
222222
111359
444444
777778
233349
*/

Working example:

https://3v4l.org/3OCkc

Original

How about this, every number is unique. Use 0 to 9 for the entropy and then shuffle.

<?php
function random_number($len = 6) {
    $num = range(0,9);
    shuffle($num);
    return implode('', array_slice($num, 0, $len));
}

for ($i=0; $i<10; $i++) {
    echo random_number(6).PHP_EOL;
}

Result:

357964
365870
392576
285196
278915
712960
751032
517420
943257
380162

Edit

Asker Comment: This I can do but the requirement is different. They need
at-least 2 unique numbers. with 1 duplicate.

<?php
function random_number_with_1_dupe($len = 6) {
    $num = range(0,9);
    shuffle($num);

    $num = array_slice($num, 0, $len-1);

    $dup = array_rand($num, 1);
    $num[] = $num[$dup];

    return implode('', $num);
}

for ($i=0; $i<10; $i++) {
    echo random_number_with_1_dupe(6).PHP_EOL;
}

564795
698756
937414
760897
706383
784626
520166
614055
798057
295809

Edit v4.0

I need a random number without these two rules : 1 unique and 5
repetitive digits. 0 unique and 6 repetitive digits. Rules applicable
: All unique. 4 unique and 2 repetitive digits. 3 unique and 3
repetitive digits. 2 unique and 4 repetitive digits.

The second argument is the rule, so you would to do:

echo random_number_with_dupe(6, mt_rand(1, 4), true).PHP_EOL;

If you want something inbetween you would define a rules array and pick out a random one.

$rules = [
    1,3,5
];

echo random_number_with_dupe(6, $rules[array_rand($rules)], true);

12

solved Generate random number of 6 digit length with at-least n unique digits in php