B
B
BonBon Slick2021-08-15 01:30:41
PHP
BonBon Slick, 2021-08-15 01:30:41

How to check the unique value generator for the probability of a collision?

There are a dozen ways to generate a string, I understand that you can put a string to generate until there is a collision, but it can be tens of millions of records.

function randomString(int $length = 16): string {
        $string = '';
        while (($len = \strlen($string)) < $length) {
            $size   = $length - $len;
            $bytes  = \random_bytes($size);
            $string .= \substr(
                \str_replace(['/', '+', '='], '', \base64_encode(sprintf('%s%s', $bytes, time()))),
                0,
                $size
            );
        }

        return $string;
    }

        $isCollisioned = false;
        $generated     = [];
        while (false === $isCollisioned) {
            $str = randomString(64);
            if (true === in_array($str, $generated, true)) {
                var_dump(count($generated));
            }
            $generated[] = $str;
        }


Maybe there are online services where you can run scripts? And then the machine can hang while the script is running.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Shumov, 2021-08-15
@BonBonSlick

Just take a virtual one anywhere and run it. What is the problem? You can also take more powerfully so that it is processed faster. You can run in multiple threads with different ranges

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question