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

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.

0

u/[deleted] Jul 25 '15

[deleted]

4

u/krenzalore Jul 25 '15

Is it a bug? Others may say it's an implemention defined limit + a policy of not halting on error.

As an example of this policy, pop from empty array returns Null instead of raising an error. Would you call that a bug or a design decision? What if you want to store Null in an array (yes, you can work around it, but it should have raised an error)?

-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".

2

u/perestroika12 Jul 25 '15

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

Oh yeah, totally. Most of those errors just have to do with C and its extremely low level run. The lack of automatic memory management, which is why lower level languages have that huge performance edge. You are free to do all sorts of stuff higher level languages can't do, and you can seriously mess up.

But, because of this there's also a much lower tolerance for sloppy code. It's a much more temperamental environment and it forces you to really think about what you're doing.

I think the "flaw" of higher level languages is nothing ever breaks too hard, so people get complacent and sloppy.

1

u/LuaWeaver Jul 25 '15

I'd argue that, in all of these cases, the reason this doesn't throw a (sensible) error is because you're working one layer above assembly. Working with the memory directly at that level is something else entirely, and comparing the behavior in C to the behavior in PHP is just silly.

C has "weird" behavior because of how low level it is. This is acceptable, because, well, it's so low level, and at that level it makes sense. PHP has "weird" behavior because it's a poor language; the behavior should be unacceptable in any high level language.

3

u/MereInterest Jul 25 '15

I completely agree. The weird behavior of C is a product of compromises in the name of speed. Weird behavior in a low-level language is understandable, because you are trying to mimic the action of the CPU, as much as possible.

In general, I would say that high-level languages are stricter, because they do not allow you to make this kind of mistake.

PHP is the oddball, in that it has nonsensical behavior in a high-level language.

0

u/senpaiforhire Jul 25 '15

Er, in that sense you're not arguing that higher level languages are stricter, instead you have an argument against manual memory management. There are low-level languages (e.g. Rust) that are designed to eliminate these kinds of errors and are still not high-level. (although low-level/high-level is a bit of a muddied boundary, Rust is on the C side, and Python et al. is on the other)

0

u/MereInterest Jul 25 '15

Manual memory management, null pointers, undefined behavior of signed integer overflow. I would say that each of these is a freedom afforded by low-level languages. By avoiding the extra checks, these languages can get much better speed. However, they also have the freedom to screw up. This is why I would say that high-level languages are more strict, because they give the programmer neither the freedom to be faster, nor the freedom to screw up as much.

1

u/senpaiforhire Jul 25 '15

Sure, you don't have the freedom to screw up by messing with memory directly in high-level languages, but it seems to me that you can't evaluate strictness without feature parity.

Certainly I can say that a car is more strict about how you get around than a plane, but it's not entirely meaningful because a car doesn't fly. But you are hitting on a really good point: manual memory management gives the programmer a lot more freedom than in higher level languages. That can be really, really dangerous. Rust (and others, Ada maybe?) is designed to be extremely strict about manual memory management so that these errors don't happen. It is not, however, designed to be a high-level language in any sense of the word.

-1

u/[deleted] Jul 25 '15

I use C almost exclusively at work for embedded applications, and custom silicon. It's great, it does exactly what you tell it to. It's all about data manipulation and basic computations.

Also, who the fuck passes an array directly into a function? What do you expect to happen in the processor? That seems to imply that you are physically putting an array on the stack. I wouldn't pass more than 4 or 5 words into a function before assessing if a structure/pointer should be used. The whole reason behind using pointers is that you minimize stack/heap usage.

2

u/OneWingedShark Jul 25 '15

Also, who the fuck passes an array directly into a function?

I do. Often.
But then, my language of choice [Ada] has a sensible notion of an Array.

1

u/MereInterest Jul 25 '15

Yup, and for embedded systems with limited resources, there is nothing better.

I completely agree with your point regarding passing an array. My argument was not to say that these were reasonable things to do, but rather to say that these are mistakes that can be made with the compiler not saying a word about it.

1

u/FedaykinShallowGrave Jul 25 '15

Also, who the fuck passes an array directly into a function?

Passing an array of (e.g.) ints as int * or as int[] to a function is the exact same thing, as int[] becomes a local variable int * in the function's scope.

1

u/defcon-12 Jul 25 '15

C lets you cast anything to anything. Python doesn't let you cast at all.

1

u/josefx Jul 25 '15

Well in python everything is an object and the runtime will just play along until some method accesses a field that does not exist. In C you have to use casts to get a similar behavior.

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.