Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mt_rand() function

Tags:

php

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];
}
?>
like image 448
zista Avatar asked Jan 05 '12 11:01

zista


People also ask

What is Mt_rand function in PHP?

The mt_rand() function is a drop-in replacement for the older rand(). It uses a random number generator with known characteristics using the » Mersenne Twister, which will produce random numbers four times faster than what the average libc rand() provides.

How do I generate a random 10 digit number in PHP?

The rand() function generates a random integer. Example tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: As of PHP 7.1, the rand() function has been an alias of the mt_rand() function.

How can I generate 5 random numbers in PHP?

rand() or mt_rand() functions can be used to Generate 5 Digit Random Number in PHP.

How do I generate a random 4 digit number in PHP?

rand() or mt_rand() functions can be used to Generate 4 Digit Random Number in PHP.


1 Answers

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

like image 74
OptimusCrime Avatar answered Sep 28 '22 07:09

OptimusCrime