Hack Forums
[PHP] Fairest way to randomly generate a number? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] Fairest way to randomly generate a number? (/showthread.php?tid=5962040)



Fairest way to randomly generate a number? - Kedox - Apr 19, 2019

I've been using mt_rand, is there any more fair ways to generate a number however?

Thanks


RE: Fairest way to randomly generate a number? - Omniscient - Apr 19, 2019

(Apr 19, 2019 03:07 PM)Kedox Wrote:  I've been using mt_rand, is there any more fair ways to generate a number however?

Thanks

What's wrong with mt_rand? Does it not do something you need?


RE: Fairest way to randomly generate a number? - Kedox - Apr 19, 2019

(Apr 19, 2019 04:41 PM)Omniscient Wrote:  What's wrong with mt_rand?  Does it not do something you need?

Trying to most fairly generate a number between 0 and 100. I was curious if it was the most fair or if there is better options


RE: Fairest way to randomly generate a number? - gianni - Apr 19, 2019

mt_rand is completely fine for what you need.

Edit:

https://stackoverflow.com/questions/28760650/difference-between-mt-rand-and-rand
enjoy the quick read on why its good


RE: Fairest way to randomly generate a number? - Kedox - Apr 19, 2019

(Apr 19, 2019 04:47 PM)gianni Wrote:  mt_rand is completely fine for what you need.

What I need it for is dice gambling, so most fair option is important


RE: Fairest way to randomly generate a number? - SuperiorUser - Apr 19, 2019

mt_rand eventually produces predictable data overtime.
https://www.soliantconsulting.com/blog/secure-randomness-php
"If you have the mcrypt library installed you could use mcrypt_create_iv() as it's believed to be a secure source of random bytes. If you are on PHP 7 you can call either random_bytes() or random_int() "
also
https://www.openwall.com/lists/announce/2013/11/04/1


RE: Fairest way to randomly generate a number? - Disease - Apr 19, 2019

(Apr 19, 2019 05:18 PM)SuperiorUser Wrote:  ...If you are on PHP 7 you can call either random_bytes() or random_int() "...

If you're not, please update or contact your host immediately and demand they update. The entire PHP 5.x branch is unsupported as of January 1st of this year. In fact, 7.0 turned end-of-life a couple months ago as well. Being on anything less than 7.1, though preferably 7.2, is not good. But yes, random_bytes() or random_int() would be a solid way to go. Both generate cryptographically secure psuedo-random return. Obviously random_int() returns a number, making it more suitable for the OP's need.


RE: Fairest way to randomly generate a number? - Charger - Apr 20, 2019

Anything you do outside of a built in function will lead to less randomness and more predictability.


RE: Fairest way to randomly generate a number? - QRCode - Apr 23, 2019




RE: Fairest way to randomly generate a number? - Squito - Apr 24, 2019

QRCode:
Quote:str_shuffle
This function does not generate cryptographically secure values, and should not be used for cryptographic purposes.

https://www.php.net/manual/en/function.str-shuffle.php
Please stop giving people bad advices/solutions.