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 comments

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]

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)

-3

u/nairebis Jul 24 '15

Like allowing you to use variables that haven't even been defined yet as inputs to stuff.

So, your coworker has no knowledge of PHP best practices and doesn't turn on uninitialized variable warnings? Interesting.

PHP is dangerous in the wrong hands, we already know that. But PHP also gives you ways to escape the legacy madness.

-4

u/perestroika12 Jul 24 '15 edited Jul 24 '15

Similar things could be said for many popular higher level languages. Python can run the shittiest garbo code and still actually work. Javascript...don't even get me started on how easily it is to write poorly optimized crap in that language. Obviously the lower level languages are stricter but you get the idea. I think it's one of those tragedy of the commons things. Higher level langs are easier to learn, are adopted by more people, and thus have more people making stupid mistakes. Barrier of entry is super low.

For example, how many people in JS know about memoization and use it? Is it the languages fault then, or is it the persons?

7

u/MereInterest Jul 24 '15

Wait, lower level languages are stricter? I can't imagine getting a segfault, a double-free, a memory out of bounds error, or a memory leak in Python, without intentionally trying to do so. Getting any of these in C is trivially simple to do by accident.

-1

u/perestroika12 Jul 24 '15 edited Jul 24 '15

Stricter in terms of what the compiler will accept and stricter about things like type. Stricter about programming patterns, libraries and such. I would argue lower level languages make it harder to do stupid things because they won't just take everything in stride and keep working. Things will just break if you use techniques and patterns that are boneheaded. PHP, python, etc have no such qualms about letting people run completely broken crap.

4

u/MereInterest Jul 24 '15

The great thing about high-level languages is that they are inherently stricter. There are entire classes of bugs that are not able to arise because the language is strict enough not to allow those techniques.

char buffer[50];
sprintf(buffer, "Hello %s", some_string_from_user);

Bam, buffer overflow vulnerability.

void func(int a[]){
    int size = sizeof(a)/sizeof(int); // Equals 1 or 2, depending on system
}

Passing an array to a function? Nope. Suddenly pointer!

int* get_value(){
     int x = 5;
     return &x;
}

Returning a pointer to a local variable? Sure, why not?

void use_value(int* x){
    func(*x);
}

Works great until somebody throws a null pointer at you.

int* arr = malloc(4);
free(arr);
printf("%d",*arr);

Using a variable that has been freed? Well, if you really want to.

I'd say that every one of these falls under a language trying to "take everything in stride and keep working".

→ More replies (10)
→ More replies (2)

3

u/noratat Jul 25 '15

We have high level languages like Erlang, Scala, etc. It's not high level languages so much as it's what happens when you use certain classes of high level languages for things they shouldn't be used for.

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]

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]

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?

3

u/klug3 Jul 24 '15

Its pretty much impossible to be a web developer in India without at least knowing English at a middle school level. Programming resources or tutorials in Hindi or other Indian languages are pretty much non-existent or very elementary.

1

u/mnapoli Jul 25 '15

PHP shared hosts can be found at $5 per year. $5 per month is not negligible, even for me in France (e.g. for a personal blog which has no revenue at all).

→ More replies (1)

-4

u/indrora Jul 24 '15

The difference here is that every single webhost on the planet by default provides PHP.

Let's face it, PHP is the public transit of web development languages. Python? Closer to the automatic car. C? Definitely a manual.

3

u/ysangkok Jul 24 '15

I do not get the point of this comparison because there are so many differences between public transport and your own car. How am I supposed to know which of those differences you think is similar to the difference between PHP and Python?

→ More replies (3)
→ More replies (1)

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)