-5

Can anyone give me the idea on implementing this logic. I know how to generate random number but not getting how to make it with at-least two unique digits.

Thank-you.

Rules Applicable :

All unique.
4 unique and 2 repetitive digits.
3 unique and 3 repetitive digits.
2 unique and 4 repetitive digits.
Need numbers forming with above four rules

Rules Not Applicable :

1 unique and 5 repetitive digits.
0 unique and 6 repetitive digits.
Numbers forming with these two rules are not necessary

Number can be like this:

123456, 334567, 444567, 555567 etc.,

These combinations are not allowed :

111111 222222 333333 444444 555555 666666 777777 888888 999999

122222 133333 .. .. 199999

222666 666777 etc.,.

Guys who are marking this question as negative and marking as duplicate can make a comment below specifying where it is explained. I explored so much before posting this question and didn't found which I need, so came here. Hope you people will help me.

Himakar
  • 345
  • 4
  • 17
  • Possible duplicate of [PHP random string generator](https://stackoverflow.com/questions/4356289/php-random-string-generator) –  Nov 30 '17 at 19:13
  • @nogad I didn't found any explanation to my question there, can you please explain it. How we can have at least two unique digits ? – Himakar Nov 30 '17 at 19:23
  • Or of https://stackoverflow.com/questions/6101956/generating-a-random-password-in-php – Kevin_Kinsey Nov 30 '17 at 19:23
  • @ThomasSmyth I am just generating a random number like this $randompassword = mt_rand(100000,999999); – Himakar Nov 30 '17 at 19:25
  • @Kevin_Kinsey It doesn't have any solution for my question. Thank-you. – Himakar Nov 30 '17 at 19:29
  • The accepted answer does indeed answer the question "how to generate a random password in PHP". You're not trying to create a password, your question is mis-labeled. You're trying to create an integer. I can't see any point in doing this, but to answer the "problem" that you're actually having, write some code, create a function that returns a number but have the function check for duplicate digits (perhaps preg_match()) before returning it. – Kevin_Kinsey Nov 30 '17 at 19:44
  • @Kevin_Kinsey Password is 6 digit integer. In-order to make password more secure we are keeping rule that it should have at-least two unique digits and the rest can be repetitive. – Himakar Dec 01 '17 at 06:23

1 Answers1

3

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);
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • This I can do but the requirement is different. They need at-least 2 unique numbers. Thanks bro. – Himakar Dec 01 '17 at 06:07
  • *They need at-least 2 unique numbers*, their **all** unique! If you want 2 **identical** numbers in the same number then, pick a random one from the range and one less in array_slice then place it in. – Lawrence Cherone Dec 01 '17 at 08:53
  • To add to that how does `123456` meet your requirement? – Lawrence Cherone Dec 01 '17 at 08:55
  • At-least two unique digits means, it can be two or three or four or five or six unique digits. So in 123456 all are unique which meets company's requirement.. – Himakar Dec 01 '17 at 18:13
  • Then so does my answer. – Lawrence Cherone Dec 01 '17 at 18:17
  • Your code generates number only with 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. Thank you for the response – Himakar Dec 02 '17 at 07:58
  • Thank you so much for your elaborate explanation brother. 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. – Himakar Dec 06 '17 at 20:47
  • I think it may not be possible with combination of those four rules. – Himakar Dec 06 '17 at 20:52
  • Just use `echo random_number_with_dupe(6, mt_rand(1, 4), true).PHP_EOL;`, please accept the answer. and stop trolling ;p – Lawrence Cherone Dec 06 '17 at 20:57
  • the second argument defines the rules, you could even have 1,3,5 you would just put them in an array and pick a random one – Lawrence Cherone Dec 06 '17 at 20:58
  • fyi, there is **no** other way to do it.. unless you pregenerate every permutation and store it. – Lawrence Cherone Dec 06 '17 at 20:59
  • yes I accept with your last comment as I tried all possible ways and came to conclusion that pre-generate and store is the only way. I just posted this question if any one shows some other way. But thank you very much for being patience with me ;)...will accept your answer now..! :) – Himakar Dec 06 '17 at 21:45