4 ms·
"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
by DonHopkins 12y ago
"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!
- AceJohnny2 12y ago> manual entry for rand understatedly mansplained Heh, I see what you did there, but the downvotes imply others didn't.
- recursive 12y agoPresumably "man" for "manpages".
- unwind 12y agoProbably part that, part joking reference to the term as used outside computing (see http://www.urbandictionary.com/define.php?term=Mansplain 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. :)
- DonHopkins 12y agoYes, 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 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_strategy_inside_the_angry_online_mens_rights_group_red_pill/ 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/openwin/man/man6/pizzatool.6 https://stuff.mit.edu/afs/net/system/sun4c_41/rsp.01/usr/ope...
- deleted 12y ago[deleted]
- recursive 12y agoI 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.
- deleted 12y ago[deleted]
- lloeki 12y agoIf 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.
- justincormack 12y agoarc4random (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/browse/libc/bionic/arc4random.c?repo=platform--bionic&r=3a654b1e04d4275ae315cfe1b196998acf10052c https://code.google.com/p/android-source-browsing/source/bro...
- tedunangst 12y agoThere 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 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.
- fulafel 12y agoThe 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.c https://github.com/lattera/glibc/blob/master/stdlib/random_r... https://www.opensource.apple.com/source/Libc/Libc-320/stdlib/FreeBSD/rand.c 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.
- tedunangst 12y agorand() and random() are different functions.