How unique is the 5-digit mt_rand () number?

I'm just wondering how unique the mt_rand () number is if you draw a 5 digit number? In this example, I tried to get a list of 500 random numbers with this function, and some of them are repeated.

http://www.php.net/manual/en/function.mt-rand.php

<?php
header('Content-Type: text/plain');

$errors = array();
$uniques = array();
for($i = 0; $i < 500; ++$i)
{
    $random_code = mt_rand(10000, 99999);
    if(!in_array($random_code, $uniques))
    {
        $uniques[] = $random_code;
    }
    else
    {
        $errors[] = $random_code;
    }
}

/**
 * If you get any data in this array, it is not exactly unique
 * Run this script for few times and you may see some repeats
 */
print_r($errors);
?>

How many digits may be required to ensure the uniqueness of the first 500 random numbers held in a loop?

+4
source share
4 answers

Why not use range, shuffle, and slice?

<?php

$uniques = range(10000, 99999);
shuffle($uniques);
$uniques = array_slice($uniques, 0, 500);

print_r($uniques);

Output :

Array
(
    [0] => 91652
    [1] => 87559
    [2] => 68494
    [3] => 70561
    [4] => 16514
    [5] => 71605
    [6] => 96725
    [7] => 15908
    [8] => 14923
    [9] => 10752
    [10] => 13816
    *** truncated ***
)

, , , . , "". ​​ , . - , ! , "" - , , .

, , mt_rand ( rand). openssl_random_pseudo_bytes, .

+5

, , . , - , , .

, , , :

$uniques = array();
for($i = 0; $i < 500; $i++) {
    do {
        $code = mt_rand(10000, 99999);
    } while(in_array($code, $uniques));
    $uniques[] = $code
}
+7

. 10000 99999 500 , .

, . , 1/6 . 3 , 4/9 (44%) . 4 , 13/18 (63,33%). , 49/54 (90,7%). , 98,5%. 100%.

20- , , . 3 14,5% . 6 69,5%. 10 96,7%, .

f(num_rolls, num_sides), , . f(num_rolls, num_sides) num_rolls num_sides -side die.

. num_rolls , num_rolls-1 , , , num_rolls-1.

f(num_rolls, num_sides) = 
  f(num_rolls-1, num_sides) * (num_sides - (num_rolls - 1)) / num_sides

,

f(num_rolls + 1, num_side) = 
  f(num_rolls, num_sides) * (num_sides - num_rolls) / num_sides

, 1 ( num_rolls , ), , num_rolls , , 0.

Google Docs, , : https://docs.google.com/spreadsheets/d/1bNJ5RFBsXrBr_1BEXgWGein4iXtobsNjw9dCCVeI2_8

90000- 500 . , , 75% , mt_rand. , , N . , 90000 , , , , 500 . , , , , N . . , . - .

+3

" ​​", , N , , ? . : " ​​" .

, , . , 365 , 90001 (= 99999-10000 + 2) , 10000 99999. , 500 , :

P (500) = 1- 90001!/(90001 ^ n (90001 - 500)!) = 0,75

Thus, there is a 75% chance that at least two of the 500 numbers you create will be the same, or in other words, only a 25% chance that you will be able to get 500 different numbers using the method that you currently time used.

As others have said, I would suggest checking for duplicate numbers in your algorithm, and not just blindly generate random numbers and hope that you have no match between any pair of numbers.

+1
source

Source: https://habr.com/ru/post/1540068/


All Articles