News for hackers. Stuff that matters.
Re:A call to PHP's mt_rand generates only odd numbers by _asummers on Friday July 24, 2015 @08:49AM (#9941568)
While documented, that is surprising behavior. If it takes in an int, shouldn't it be able to take in PHP_INT_MAX? And shouldn't it yell at you instead of just silently going about its day?
Re:A call to PHP's mt_rand generates only odd numbers by mikeash on Friday July 24, 2015 @08:59AM (#9941621)
The PHP approach seems to be that any crazy behavior is acceptable as long as it's documented.
Re:A call to PHP's mt_rand generates only odd numbers by dspillett on Friday July 24, 2015 @09:52AM (#9941966)
To be fair, that is not an uncommon situation elsewhere either.
Re:A call to PHP's mt_rand generates only odd numbers by mikeash on Friday July 24, 2015 @11:01AM (#9942441)
I think it is. Sure, there are plenty of calls out there in all sorts of systems that have crazy, documented behavior. But it's rare to see anyone outside of the PHP world defending the crazy behavior purely on the basis that it's documented. Crazy behavior is almost always there because of compatibility concerns, performance concerns, or an acknowledged bug that just hasn't been fixed yet. Outside of PHP, I rarely see anyone just say, "It's fine, that's how it's documented to behave" and leave it at that.
Re:A call to PHP's mt_rand generates only odd numbers by Peaker on Friday July 24, 2015 @01:45PM (#9943801)
I recall a Rails bug used to hack github and inject a bugfix commit. The Rails community rejected the fix originally because the crazy behavior was documented.
Re:A call to PHP's mt_rand generates only odd numbers by mkehrt on Friday July 24, 2015 @02:07PM (#9943954)
Wow. Do you have any more info on this?
Re:A call to PHP's mt_rand generates only odd numbers by dkns on Friday July 24, 2015 @02:58PM (#9944352)
Re:A call to PHP's mt_rand generates only odd numbers by BuildTheRobots on Friday July 24, 2015 @11:58AM (#9942860)
Outside of php and systemd.....

<Friday afternoon trolling is just too easy this week />

Re:A call to PHP's mt_rand generates only odd numbers by dspillett on Friday July 24, 2015 @12:43PM (#9943236)
And mySQL of old.

I assume things are much better now, but I remember a time when things like INSERT <table> (<date_field>) VALUES ('2015-02-30') would not have raised any sort of error, amongst other terrible things that people would defend having to implement explicit checks for in other layers of your application.

Re:A call to PHP's mt_rand generates only odd numbers by slapresta on Friday July 24, 2015 @02:32PM (#9944181)
> I assume things are much better now

Nope: http://grimoire.ca/mysql/choose-something-else

Re:A call to PHP's mt_rand generates only odd numbers by Spivak on Friday July 24, 2015 @02:27PM (#9944140)
I'm genuinely curious, what are some examples of systemd's odd behavior?
Re:A call to PHP's mt_rand generates only odd numbers by BuildTheRobots on Friday July 24, 2015 @04:07PM (#9944824)
Well, the fact my window manager is now intrinsically linked to my systems boot process strikes me as somewhat obscene. There was the amusement with it reacting to debug being passed to the kernel which caused the system to never finish booting...

systemd is by no means alone; in the last month I've discovered that `yum` deliberately breaks its output when you try and pipe it to something else (I mean really, ffs, really?). The justification for this isn't even internally consistent which really winds me up too...

Whilst I pick on a couple, there's been innumerable packages, languages and platforms over the years that have had their share of what could politely be called idiosyncrasies. I highly recommend The Unix Haters Handbook [1] for plenty of historic examples -it's a lovely read -oh, and for years one of the *nix bullet points was always "all the power in the world and not a safety in sight" [2]

[1] The Unix Haters Handbook: https://en.wikipedia.org/wiki/The_Unix-Haters_Handbook || http://www.csf.ac.at/fileadmin/user_upload/BioComp/training/...

[2] The Hole Hawg: https://web.archive.org/web/20150214132948/http://www.team.n...

Re:A call to PHP's mt_rand generates only odd numbers by RickHull on Friday July 24, 2015 @07:41PM (#9945891)
> Well, the fact my window manager is now intrinsically linked to my systems boot process strikes me as somewhat obscene.

Gnome depends on logind, which is sensible. Yes, logind is part of a suite of system management tools which also includes tools to manage the boot process.

Your complaint would apply equally to your window manager being intrinsically linked to your serial port (via the Linux kernel).

Re:A call to PHP's mt_rand generates only odd numbers by digi_owl on Saturday July 25, 2015 @01:55PM (#9948363)
Not just part of, logind will straight up not work if systemd is not running as pid1. As such, anything that depends on logind is dependent on a specific init sitting as pid1.
Re:A call to PHP's mt_rand generates only odd numbers by BuildTheRobots on Saturday July 25, 2015 @08:12AM (#9947353)
As an honest question, could you explain _why_ it's sensible?

Until a couple of years ago, my linux box was nearly entirely modular letting me install any combination of boot-loader, kernel, init system userland tools and/or desktop. How is this better?

Re:A call to PHP's mt_rand generates only odd numbers by pogden on Friday July 24, 2015 @12:38PM (#9943187)
What's more common outside PHP is that functions throw errors or exceptions when given invalid input, rather that trucking along and producing output that is clearly not what was intended.
Re:A call to PHP's mt_rand generates only odd numbers by Natsu on Friday July 24, 2015 @06:01PM (#9945466)
Maybe they should just make the random functions return '4', documenting that it was chosen by a fair dice roll and is therefore guaranteed to be random.
Re:A call to PHP's mt_rand generates only odd numbers by sarciszewski on Friday July 24, 2015 @10:43AM (#9942307)
That's not a "crazy behavior", it's a limitation of the algorithm they use. mt_rand() only has approx. 2^32 possible seeds; why would you expect it to support ranges larger than 2^32?

The best thing to do is to not use mt_rand().

Re:A call to PHP's mt_rand generates only odd numbers by SeanLuke on Friday July 24, 2015 @12:04PM (#9942921)
> why would you expect it to support ranges larger than 2^32?

Because this is not a simple, stupid linear congruential generator. mt_rand(), is, as I understand it, based on Mersenne Twister, a well-regarded generator which has a period of 2^19937. And if PHP had done this right, mt_rand() would be seedable with an array of some 624 integers.

Mersenne Twister has an alternative "classic" seeding algorithm, drawn from Knuth, to be compatible with old-style generators. This algorithm takes a 32-bit seed. Apparently that's all that PHP is supplying.

It's dead simple to do random(n) uniformly. When I heard that some language implemented a function called mt_rand() which was hilariously broken in this respect, the first thing that came to my mind was "that's gotta be PHP". And it was.

Re:A call to PHP's mt_rand generates only odd numbers by mikeash on Friday July 24, 2015 @10:59AM (#9942426)
I would not expect it to support ranges larger than 2^32, so why does it?

Returning garbage when one of your parameters is beyond a certain limit is "crazy behavior." Either support it properly, or don't support it at all.

Re:A call to PHP's mt_rand generates only odd numbers by Spivak on Friday July 24, 2015 @02:32PM (#9944178)
If we were talking about C this would be called undefined behavior. If you don't satisfy the preconditions before calling a function then the not only is the output invalid but the entire program which uses it is invalid too.

I don't think a language like PHP, or indeed anything written in it, should have undefined behavior but I'm not the author of the library.

Re:A call to PHP's mt_rand generates only odd numbers by pilif on Friday July 24, 2015 @11:46AM (#9942769)
"Fixing" broken data silently is tremendously bad behaviour in general because you can't possibly know whether the caller knew that they were providing invalid data and whether the caller is going to be happy with your fix.

If you expect input parameters in some range and you get values out of that range, then you blow up. You don't silently truncate anything and you certainly don't reduce the entropy of your random number generator by nearly 50%.

Re:A call to PHP's mt_rand generates only odd numbers by FranOntanaya on Friday July 24, 2015 @12:09PM (#9942956)
This was my main doubt about PHP 7's scalar types, which will autocast values into the desired types. I don't expect the caller to know better than the callee what are the method's boundaries, and once casted you may not be able to see if the input was garbage. But yeah, when something breaks the method's author can wash their hands.
Re:A call to PHP's mt_rand generates only odd numbers by skybrian on Friday July 24, 2015 @10:55AM (#9942387)
If the user asked for more bits of randomness, they need to generate more bits. They could have chosen a different algorithm, or perhaps called the random number generator twice and concatenated the bits. Scaling up is clearly the wrong thing to do.
Re:A call to PHP's mt_rand generates only odd numbers by mikeash on Friday July 24, 2015 @11:03AM (#9942455)
It also makes me wonder how it behaves for numbers below the limit. I would wager that there's substantial bias in the results if you ask for a max of, say, 2^31 + 2^30.
Re:A call to PHP's mt_rand generates only odd numbers by pogden on Friday July 24, 2015 @12:21PM (#9943052)
The crazy part is that, instead of throwing an error when given input that doesn't make sense, it just silently produces garbage output.
Re:A call to PHP's mt_rand generates only odd numbers by alkonaut on Friday July 24, 2015 @01:36PM (#9943711)
This. Documenting odd behavior like this isn't an excuse for having odd behavior. It should just be changed to fail on bad input. It's not even a breaking change (apart from code already using it with bad input).
Re:A call to PHP's mt_rand generates only odd numbers by peterfirefly on Friday July 24, 2015 @09:58AM (#9941994)
How else is it going to unseat INTERCAL?
Re:A call to PHP's mt_rand generates only odd numbers by vidarh on Friday July 24, 2015 @10:22AM (#9942147)
First it needs a replacement for INTERCAL's "please come from".
Re:A call to PHP's mt_rand generates only odd numbers by juliangregorian on Friday July 24, 2015 @11:44AM (#9942753)
A clear example of "worse is better" winning.
Re:A call to PHP's mt_rand generates only odd numbers by brador on Friday July 24, 2015 @03:20PM (#9944512)
If it's documented it's a feature.
Re:A call to PHP's mt_rand generates only odd numbers by leavinit on Friday July 24, 2015 @03:52PM (#9944713)
Re:A call to PHP's mt_rand generates only odd numbers by sneak on Friday July 24, 2015 @08:57AM (#9941617)
Almost everything in PHP is surprising behavior.

And yes, in the eleventy billion cases where PHP should yell at you that it's doing something totally different than what you wanted, it instead just goes about its day silently.

Re:A call to PHP's mt_rand generates only odd numbers by KingMob on Friday July 24, 2015 @09:42AM (#9941913)
Which is why it was a great fit for MySQL, which would silently store invalid dates and truncate over-long strings with no warnings.
Re:A call to PHP's mt_rand generates only odd numbers by mratzloff on Friday July 24, 2015 @10:49AM (#9942350)
You joke, but I'm pretty sure this is one of the reasons why that stack was so successful. Same principle behind HTML rendering. "It's displaying something, isn't it? Errors are only suggestions."
Re:A call to PHP's mt_rand generates only odd numbers by krick on Friday July 24, 2015 @03:14PM (#9944456)
I'm not so sure. It's kind of true for HTML (forgiveness was not "good" property of the format, but of the browser: 1 site works in one browser, but not in the other, and user will blame the browser, so let's display whatever the hell we can). But PHP in the early days was kinda handy (even though hacky) tool, allowing to use simple syntax to render HTML instead of custom Perl-scripts. And then it just got popular.

MySQL was once actually superior open-source database, being more performant and simpler to setup and use than PostgreSQL. And then, once again, it just got popular.

I don't think being faulty helped these technologies, although it sometimes is the case.

Re:A call to PHP's mt_rand generates only odd numbers by k_roy on Friday July 24, 2015 @11:00AM (#9942427)
There are warnings. Most people just ignore them in their APIs
Re:A call to PHP's mt_rand generates only odd numbers by KingMob on Thursday July 30, 2015 @12:19PM (#9976018)
Trying to insert over-large data should have been an error by default from the outset, never a warning. Ilooked at MySQL in 2000 and went "You had one job, database; protect my data."
Re:A call to PHP's mt_rand generates only odd numbers by vog on Friday July 24, 2015 @10:17AM (#9942128)
This is so true. It is the main reason I switched to PostgreSQL and never looked back.
Re:A call to PHP's mt_rand generates only odd numbers by gmac on Friday July 24, 2015 @10:44AM (#9942317)
... and accept nonsensical SQL queries, returning whatever it feels like.
Re:A call to PHP's mt_rand generates only odd numbers by coldtea on Friday July 24, 2015 @05:21PM (#9945270)
>Almost everything in PHP is surprising behavior.

It's not. White it does have several surprising behaviors, "almost everything" is just an example of uniformed BS people say when it comes to PHP.

C also has undefined behavior and several bad decisions, ditto for C++, Javascript, etc. But PHP is just an easy target for trolls.

Re:A call to PHP's mt_rand generates only odd numbers by sarciszewski on Friday July 24, 2015 @10:46AM (#9942327)
Even worse, some of the Internals teams argue in favor of silently trucking along instead of, e.g. throwing an exception: http://www.serverphorums.com/read.php?7,1250372

EDIT: because of HN's annoying "you are posting too fast" limit I cannot reply to the comment below.

The arguments against Exceptions began in the comments on the relevant pull request: https://github.com/php/php-src/pull/1397

Re:A call to PHP's mt_rand generates only odd numbers by _lce0 on Friday July 24, 2015 @12:02PM (#9942901)
from that thread..

   At what point do we stop blaming the developers for not knowing how to
   use our badly designed features, and accept responsibility for exposing
   an API that is hostile towards simple, efficient, and correct implementations?
Re:A call to PHP's mt_rand generates only odd numbers by paragon_init on Friday July 24, 2015 @12:23PM (#9943078)
It might be worth noting that 'sarciszewski was the author of that remark.
Re:A call to PHP's mt_rand generates only odd numbers by juliangregorian on Friday July 24, 2015 @11:38AM (#9942708)
Wow, that thread is current! This mentality really is systemic to PHP. It's like the exact opposite of "let it crash".
Re:A call to PHP's mt_rand generates only odd numbers by claar on Friday July 24, 2015 @11:59AM (#9942876)
Actually that thread seems to strongly argue that either an Exception or fatal error should be thrown. I'm not seeing anyone in that thread wanting to silently truck along -- am I missing it?
Re:A call to PHP's mt_rand generates only odd numbers by jwatte on Friday July 24, 2015 @08:35PM (#9946059)
That is not the PHP way.
[deleted]
Re:A call to PHP's mt_rand generates only odd numbers by Navarr on Friday July 24, 2015 @09:21AM (#9941754)

    php > echo PHP_INT_MAX;
    9223372036854775807
Re:A call to PHP's mt_rand generates only odd numbers by k8tte on Friday July 24, 2015 @09:26AM (#9941788)
the error is from php 4.2 or something.
Re:A call to PHP's mt_rand generates only odd numbers by saucetenuto on Friday July 24, 2015 @10:37AM (#9942259)
Ah, I see, I just misunderstood their interface. Thanks.