News for hackers. Stuff that matters.
Re:The bell has tolled for rand() by DonHopkins on Thursday February 12, 2015 @01:03AM (#9037644)
"The C random library has always been… to put it politely… less than ideal. Okay, it’s pretty fucking horrible. It’s so bad that the C standard itself suggests you’d be better off not using it."

Back in the days 4.2 BSD or so, the BUGS section of the manual entry for rand understatedly mansplained that it had "bad spectral characteristics". In fact, it was so bad that the lower bit alternated between 0 and 1 every time you called it. Hard to miss that bright line on a spectrogram.

If you couldn't figure out what to expect from such a forthright disclosure in the manual, then you were in for quite a shock when you did the obvious thing and tried to use "rand() & 1" to simulate flipping a coin!

Re:The bell has tolled for rand() by fulafel on Thursday February 12, 2015 @05:32AM (#9038234)
The claim that the C standard suggests not using it is disingenuous: The footnote in question just says that there aren't implementation quality guarantees and specific requirements can be met with application specific RNGs. This applies to most of the standard library...

Also the anecdote about bad rand() in a 1983 BSD libc says little about current rand() implementations!

These look ok, for example:

https://github.com/lattera/glibc/blob/master/stdlib/random_r...

https://www.opensource.apple.com/source/Libc/Libc-320/stdlib...

Of course it's true that relying on libc implementation quality is always risky for maximally portable programs. Same can be said for stdio, malloc, etc etc.

Re:The bell has tolled for rand() by tedunangst on Thursday February 12, 2015 @07:46AM (#9038518)
rand() and random() are different functions.
Re:The bell has tolled for rand() by lloeki on Thursday February 12, 2015 @03:13AM (#9037932)
If I were to use C, what should I be using instead of rand(3)?

A cursory look at rand(3) SEE ALSO hints at candidates but random(3) seems to hardly fare better†, arc4random(3) isn't available on glibc.

† A few notes about rand as the parent suggests, but nothing regarding mod bias, srandom initial state, or threads:

> It returns successive pseudo-random numbers in the range from 0 to (231)-1. The period of this random number generator is very large, approximately 16((2*31)-1).

> All of the bits generated by random() are usable. For example, `random()&01' will produce a random binary value.

Re:The bell has tolled for rand() by tedunangst on Thursday February 12, 2015 @05:10AM (#9038184)
There are several paths to getting arc4random or something like it.

You can just use it and optionally tell people to link with e.g. libcrypto from libressl.

You can include the portable code yourself, though that's yucky. Aging software that includes never updated very early versions of arc4random is actually kind of a problem because that code still gets used when better versions are available.

You can link with https://github.com/nmathewson/libottery-lite which is approximately arc4random with a different name.

Some combination of the above.

For example, sqlite3 includes an arc4random workalike (rc4 rng), but doesn't discard the early stream (critical because it leaks the key) nor include any degree of fork safety. Nor does it check if the host provides a better version. sqlite3 shipped with OpenBSD is patched to use libc arc4random instead, but building from source means you're back to square one. Not a big deal in the case of sqlite3, but try not to build something that doesn't improve as the world around it improves.

Re:The bell has tolled for rand() by justincormack on Thursday February 12, 2015 @04:38AM (#9038106)
arc4random (under a different name as the name is misleading) will be standardised, and Linux now has a random() syscall that is a necessary building block, so it doesnt fail.

In the meantime, use arc4random on openbsd and netbsd 7 and read from /dev/urandom on all other platforms (FreeBSD is in process of fixing arc4random, currently still uses rc4 and may have problems with state being same in multiple threads; I guess OSX is the same version).

Android does have arc4random, although it can fall back to weaker random sources if it does not manage to open /dev/urandom https://code.google.com/p/android-source-browsing/source/bro...

Re:The bell has tolled for rand() by noselasd on Thursday February 12, 2015 @05:19AM (#9038209)
How well does the rand48 family of functions fare ? http://pubs.opengroup.org/onlinepubs/7908799/xsh/drand48.htm...
Re:The bell has tolled for rand() by AceJohnny2 on Thursday February 12, 2015 @02:27AM (#9037846)
> manual entry for rand understatedly mansplained

Heh, I see what you did there, but the downvotes imply others didn't.

Re:The bell has tolled for rand() by recursive on Thursday February 12, 2015 @03:04AM (#9037919)
Presumably "man" for "manpages".
Re:The bell has tolled for rand() by unwind on Thursday February 12, 2015 @03:22AM (#9037953)
Probably part that, part joking reference to the term as used outside computing (see http://www.urbandictionary.com/define.php?term=Mansplain).

I guess the idea is that manual pages can sometimes seem to be intentionally obscure, and kind of proud of it. I don't have an example handy, nor am I even sure I agree, but I think I got the joke, at least. :)

Re:The bell has tolled for rand() by DonHopkins on Thursday February 12, 2015 @06:46AM (#9038394)
Yes, that was my intent: the manual entry used the term "bad spectral characteristics" condescendingly, instead of actually admitting that rand was a terrible mistake and nobody should ever use it. The effort the manual writer put into rationalizing the bug would have been better put into fixing it.

I looked up "mansplaining" on the urban dictionary while writing that, and was disgusted to see that it was full of sexist definitions written by obviously butt-hurt men's rights advocates, literally blaming angry misandrist pseudo-intellectual ugly insecure radical fat women for using it as a get-out-of-jail-free card against daring opinionated oppressed men. http://www.urbandictionary.com/define.php?term=mansplaining

So it's possible the downvotes came from red pill poppers reflexively reacting to my use of the word "mansplaining", and if so, I suffer those fools gladly, and hope to earn their downvotes again. http://www.salon.com/2014/07/01/feminism_is_a_sexual_strateg...

Here's a Unix manual entry that I wrote, which made it into Solaris and SVR4, whose BUGS section is now obsolete thanks to technological advances based on high quality pseudo random number generators, like https and PayPal: https://stuff.mit.edu/afs/net/system/sun4c_41/rsp.01/usr/ope...

Re:The bell has tolled for rand() by recursive on Thursday February 12, 2015 @03:00PM (#9041009)
I mean, in general, if I see the term "mansplain" on the internet with no context, I'm going to assume the usage is in line with what's described on urban dictionary. Just because that's how it's actually predominantly used. This is the first usage I've ever seen that was something else. So it's not totally unreasonable.