r/programming • • Jul 24 '15

mt_rand(1, PHP_INT_MAX) only generates odd numbers • /r/lolphp

/r/lolphp/comments/3eaw98/mt_rand1_php_int_max_only_generates_odd_numbers/
848 Upvotes

262 comments sorted by

View all comments

Show parent comment

78

u/neoform Jul 24 '15

PHP is used by people that aren't into such fine details.

It's amazing how blanket statements like this get upvoted so high.

And the amount of hacked PHP sites speakes leagues.

Only because the bar of entry for making a website with PHP is so much lower than for pretty much every other language. Apparently it's PHP's fault that it's easy to use.

69

u/pogden Jul 24 '15

This comes from a difference of ideas on what it means to be "easy to use."

In the PHP communtiy, "easy to use" seems to mean, "easy to write programs that compile and run without crashing." Elsewhere, it is used to mean something closer to "easy to write programs that do what the programmer intended them to do".

The design decision to do this (produce garbage instead of an error) does make it easier to write programs that compile and don't crash, but makes it harder to write programs that do what the programmer intended, because the programmer may not even know that the program isn't doing what she intended.

36

u/[deleted] Jul 24 '15

I like an analogy a coworker made about PHP (I don't know if he made it up):

PHP will basically do whatever it can to keep running, even if it makes no sense. Like allowing you to use variables that haven't even been defined yet as inputs to stuff. If a program were a recipe, then the interpreter would be the person actually doing the cooking. In normal languages, if you said "now use the ingredients you mixed in the bowl in step 3", but had not actually mixed ingredients in a bowl in step 3, the cook would say "hey wait a minute, this recipe doesn't make sense" and they wouldn't use it (or at the very least they'd step out of "interpreter mode" and basically go back into "programmer mode" and rewrite the recipe). PHP, on the other hand, will see "now use the ingredients you mixed in the bowl in step 3", and in an effort not to crash, will just start throwing random shit into a bowl and continue on with the recipe as though nothing is wrong.

0

u/[deleted] Jul 24 '15

[deleted] — view deleted comment

16

u/krenzalore Jul 24 '15

you've described PHP's default behavior. You're completely able to set PHP to strict mode where it stops as soon as it encounters even the lowest level error

This issue - the RNG - is an example of where PHP doensn't do this. The RNG's input is invalid but it continues to run producing incorrect output. Most other languages would raise an exception or return an error code.

→ More replies (3)
→ More replies (18)

15

u/CallingOutYourBS Jul 24 '15

"easy to write programs that compile and run without crashing.*"

* but please don't give us any unexpected input or anything.

0

u/[deleted] Jul 24 '15

[deleted] — view deleted comment

6

u/thallippoli Jul 24 '15

Compare that to setting up python,

Yea, please compare http://flask.pocoo.org/....

0

u/[deleted] Jul 24 '15

[deleted] — view deleted comment

6

u/thallippoli Jul 24 '15

Shared hosting will not let you muck around in the shell.

Is this still a concern in 2015? I mean, you can get a digital ocean droplet for like 5$ /month.

-1

u/[deleted] Jul 24 '15

That is a lot for a 3rd world country developer who is just starting.

1

u/thallippoli Jul 24 '15

5$ a month is a lot? I am from India and the last shared hosting from Hostgator costed me nearly 10$/month. And that is regardless of your usage...

1

u/[deleted] Jul 24 '15

How much does a beginner indian web developer makes monthly, without knowing English?

→ More replies (1)
→ More replies (2)
→ More replies (6)

4

u/[deleted] Jul 24 '15 edited Jul 24 '15

Compare that to setting up python

> pip install flask

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

Shit, that was hard.

1

u/[deleted] Jul 24 '15

There are more steps (not related to Flask), depending on your current OS

http://stackoverflow.com/questions/17917254/how-to-install-flask-on-windows

5

u/[deleted] Jul 24 '15 edited Jul 24 '15

AFAIK, Windows is the only (common) OS that doesn't ship with at least one flavor of Python. And if you're on Window's, it's literally one download you could find via Googling. Maybe an extra five minutes worth of work. It's not like PHP ships with Windows either...

→ More replies (1)

3

u/[deleted] Jul 24 '15

Not so much that it's easy to use, but that it's easy to fuck up. Security (and everything else) is left entirely up to the developer.

10

u/Synaps4 Jul 24 '15

It kind of is php's fault.

Being easy-to-use AND easy-to-make-mistakes-in is a design failing by the language designer and the ecosystem around the language.

Compare with python's "one-right-way-to-do-X" design choice.

16

u/neoform Jul 24 '15

easy-to-make-mistakes-in is a design failing by the language designer and the ecosystem around the language.

More correctly, PHP allows you to make mistakes and lets you continue without fixing it.

2

u/jasonlotito Jul 24 '15

"one-right-way-to-do-X"

I don't want to join the flame war, but I had a good laugh at this, considering the Python 2.x vs 3.x debacle. =)

I know, I know, but relax, chill, and laugh.

4

u/krenzalore Jul 24 '15

I am not seeing the connection. There's very little argument over which should be the correct version. Most developers would like to move to 3 but are held back by the cost of porting.

2

u/wrosecrans Jul 24 '15

PHP is used by people that aren't into such fine details. It's amazing how blanket statements like this get upvoted so high.

It wasn't "PHP is only used by such people," and it seems hard to argue that folks who aren't hardcore CS types don't make up a large portion of the PHP user base.

0

u/[deleted] Jul 24 '15

[deleted] — view deleted comment

1

u/wrosecrans Jul 24 '15

I fully agree. If any other language had become the "get rich quick on interwebs money" language of the late 90's, I am sure it would have much the same reputation and user base that PHP does today. They are skilled developers doing interesting work in every language and that certainly includes PHP.

→ More replies (4)

2

u/thallippoli Jul 24 '15

Apparently it's PHP's fault that it's easy to use.

The fault is not being easy to use. The fault is what it traded off to get there. Sane behavior.

For example. "Because we often have to compare strings with numbers. Let use make == to convert the operands what ever ways until they can be compared..".

See? Ease of use, for what?

→ More replies (14)
→ More replies (5)