zista
zista

Reputation: 197

PHP mt_rand() function

Are PHP random numbers predictable? if so, how difficult would it be to predict random numbers that are in a range of 1 to 32? and is there any way to make it unpredictable?

<?php
function rand_best($min, $max) {
    $generated = array();
    for ($i = 0; $i < 100; $i++) {
        $generated[] = mt_rand($min, $max);
    }
    shuffle($generated);
    $position = mt_rand(0, 99);
    return $generated[$position];
}
?>

Upvotes: 5

Views: 1079

Answers (3)

Kenan Duman
Kenan Duman

Reputation: 164

After PHP 7 version you can use random_int() too.
Take a look at this: http://php.net/random_int

Upvotes: 1

OptimusCrime
OptimusCrime

Reputation: 14863

The discussion around how random random-functions in programming is, is ancient.

Take a look at this: http://en.wikipedia.org/wiki/Random_number_generation

Anyway. The random-functions are so good today that they are (what I would call) as near random as possible. There's no way to predict a result between 1,32 (or any other number for that sake). The deal is that the numbers are not truly random because a computer can not do such a operation.

I'd say the rand-functions is more than good enough unless you are writing something for Pentagon

Upvotes: 7

Assuming a Linux system, you could seed your pseudo random number generator with /dev/urandom (or read from that), or possibly /dev/random (be careful, it can block).

Upvotes: 2

Related Questions