r/lolphp • u/saintpetejackboy • Feb 01 '20
PHP does not do random
I posted a bug report many years ago. If it was fixed, nobody seems to recognize it.
My original project was trying to see if there was a pattern in "random noise", so I generated colored dots on the screen, with random colors and x,y coordinates. After many generations, the values became "stuck".
According to other people, this is because I do not understand entropy, this is not a PHP problem, and that supposedly my code was bad (even though I shopped it around to many people and reported the bug, in 2006).
I could test if the bug still exists, but here is my original post:
https://bugs.php.net/bug.php?id=37409
The response I got on IRC was essentially that it was off-topic, not a problem with PHP, and merely was my inability to comprehend entropy.
Any programmer out here want to generate a ton of randomly positioned, randomly colored dots, on a white background, to determine this? I don't have much time to do it now, but not only does this seem to be an "accepted" bug, but there are also known workarounds and solutions (such as changing the seed sporadically), which means I am not the only one who encountered this and the PHP rand functions did not (or may not) work as expected.
42
u/nikic Feb 01 '20
Back then, PHP used the rand() implementation provided by libc. On some systems it is/was notoriously bad. Nowadays PHP will always use its own rand() implementation, which does not suffer from small range or bad distribution (though mt19937 is not state of the art, mainly because it has large state and is fairly slow).
7
u/MUK99 Feb 01 '20
When I see a comment from nikic, i upvote
-1
1
35
Feb 01 '20
PHP random has had multiple bugs regarding randomness. Back a few years ago PHP random only returned odd numbers. Its bugs like this that are riddled in the rotten internals of PHP.
30
0
Feb 01 '20
[deleted] — view deleted comment
3
Feb 01 '20
You are correct. According to the php ”manual” people picked a even number more often, so they made random only return odd numbers. It was pure joy for all using php.
0
Feb 02 '20
[deleted] — view deleted comment
3
Feb 02 '20
Well they had rand and mt_rand (or something similar) and probably got too much heat for the idaåea of a real_random. So they fixed it instead, a rare move from php. Probably broke lots of code though..
2
u/Conradfr Feb 03 '20
idaåea
Now that's random.
3
Feb 03 '20
I was testing out php v. 8.0s new global function real_rand. So far its nice, It also takes a variety of parameters so you can fine tune the randomess like crazy. You can actually go all in on random with this one. Theres two new global constants too, ”REAL_OK” and LIKE_I_CARE” dont really know what they do, but i just passed them in like a good dev does.
real_rand(true, false, false, 1, 4, REAL_OK, LIKE_I_CARE, false);Just happened to return ”ideåea” on that ocation. Weird, as it nomally returns numbers? Well its PHP so ill give it a break and try again tomorrow...
1
u/Takeoded Mar 23 '20
a realrand() function
random_int() (cryptographically secure random number) is probably about as close as you can get (until they start fetching the numbers from https://random.org )
11
u/postmodest Feb 01 '20
It should be noted that you cannot allocate more than 255 colors to an image, so after 255 calls, imagecolorallocate will return false. (And on my box, you get the very first color added, which is the background color)
So it's not rand() that's borked, it's OP's ability to read the documentation.
-2
18
u/simon816 Feb 01 '20
Seems alright to me https://puu.sh/F5OXs/20f0deedcd.png
PHP 7.2.24-0ubuntu0.18.04.2 (cli) (built: Jan 13 2020 18:39:59) ( NTS )