How can I get fully randomized numbers from a PHP script?
Asked
Active
Viewed 139 times
-3
-
1[random_int](http://php.net/manual/en/function.random-int.php) – Paul Apr 12 '16 at 06:02
-
http://stackoverflow.com/questions/1041509/generate-cryptographically-secure-random-numbers-in-php – Gordon Apr 12 '16 at 06:03
-
2Yeah how about you take a quick tour how to [ask a good question](http://stackoverflow.com/help/asking) and maybe you try out this gorgeous tool named [google](http://www.google.com) – ckruczek Apr 12 '16 at 06:04
-
Depends on what you mean by "fully randomized". If you want true random numbers, you'll have to use a specialized hardware or an external service; "normal" computer hardware can't generate true randomness. – JJJ Apr 12 '16 at 06:08
-
Just by typing "PHP random number" in a web search yields thousands of results with readily usable scripts which you can just copy + paste & modify. Try consulting a good PHP manual, like SAMS PHP in 24 hours. – Apr 12 '16 at 06:09
-
Possible duplicate of [Random number in range \[min - max\] using PHP](https://stackoverflow.com/questions/4173867/random-number-in-range-min-max-using-php) – Oct 15 '17 at 10:09
1 Answers
-1
mt_rand()
should do it (PHP5 code).
random_int()
works only starting with PHP7.

Andreas
- 2,821
- 25
- 30
-
2You can use `random_int` in PHP 5 too, [with a polyfill](https://packagist.org/packages/paragonie/random_compat). – Paul Apr 12 '16 at 06:10
-
@Paulpro: Unless he has good reasons why he needs cryptographically secure random integers, I wouldn't suggest to blow your application code with unnecessary packages. His question sounds to me like he just wants to have a random integer. – Andreas Apr 12 '16 at 06:16
-
2`mt_rand` is a PRNG not a CSPRNG. It definitely couldn't be used "for casinos". It also takes only a few seconds to add `random_compat` as a project dependency and keeps the code future proof. – Paul Apr 12 '16 at 06:23