Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

Fairest way to randomly generate a number?

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

Thanks
(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?
(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
mt_rand is completely fine for what you need.

Edit:

https://stackoverflow.com/questions/2876...d-and-rand
enjoy the quick read on why its good
(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
mt_rand eventually produces predictable data overtime.
https://www.soliantconsulting.com/blog/s...omness-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
Whitehat verified.
(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.
<Disease />
Anything you do outside of a built in function will lead to less randomness and more predictability.
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.
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by EffexionAug 2, 2019 03:04 PM 5 210 Aug 2, 2019 03:04 PM
Last Post: Nachfolger
[PHP] by deadsh0tJan 31, 2018 06:45 AM 3 146 Jan 31, 2018 06:45 AM
Last Post: deadsh0t
[PHP] by CodeBinaryApr 16, 2016 03:27 PM 33 778 Apr 16, 2016 03:27 PM
Last Post: sarciszewski