TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The bell has tolled for rand()

80 pointsby StylifyYourBlogover 10 years ago

6 comments

jlebarover 10 years ago
Not to focus on a question unrelated to this blog post&#x27;s point, but<p>* &quot;auto main() -&gt; int&quot; could be just &quot;int main()&quot;.<p>* &quot;auto v = vector&lt;int&gt;(20);&quot; could be just &quot;vector&lt;int&gt; v(20)&quot;.<p>* &quot;auto print_value = [](auto&amp;&amp; v)&quot; could be (I&#x27;d argue <i>should</i> be) &quot;auto print_value = [](const auto&amp; v)&quot;.<p>&quot;auto&quot; is useful, &quot;auto&quot; is great. But &quot;auto&quot; is not an end in and of itself.<p>(Bring on the C++(11) haters, blah blah.)
评论 #9038186 未加载
评论 #9037908 未加载
gpvosover 10 years ago
Interesting to see that OpenBSD recently went the other way: break the standard and make rand() a good random generator by default. Even to the point of making srand(time(0)) a no-op.<p><a href="https://news.ycombinator.com/item?id=8719593" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=8719593</a>
评论 #9038484 未加载
评论 #9038438 未加载
DonHopkinsover 10 years ago
&quot;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.&quot;<p>Back in the days 4.2 BSD or so, the BUGS section of the manual entry for rand understatedly mansplained that it had &quot;bad spectral characteristics&quot;. 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.<p>If you couldn&#x27;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 &quot;rand() &amp; 1&quot; to simulate flipping a coin!
评论 #9038234 未加载
评论 #9037932 未加载
评论 #9037846 未加载
leni536over 10 years ago
Why is a specific random generator should be in the standard at all? Random generators are quite a dangerous area.<p>Picking one is not without compromises: Do you want your PRNG fast? Or do you want it cryptographically secure?<p>It could become obsolete in less time than expected. It&#x27;s mostly true for CSPRNGs. Maybe that&#x27;s why they are considering Mersenne Twister which is &quot;good enough&quot; for many use cases but not meant to be cryptographically secure. Sure, I&#x27;m using it right now for physics simulation and it&#x27;s certainly good enough for that and it&#x27;s hard to imagine a case where a 623-dimansionally equidistributed PRNG could fail. But it most certainly can if it can&#x27;t be used for cryptography. It is <i>much</i> better than rand() though and one could argue that it will be good enough 99% of the time. The problem that you can&#x27;t replace it if it ever becomes obsolete since some software depends on the predictability and still random like features of PRNGs (like game map generators, digital art, etc...).<p>I think the one boost library they should standardize is boost&#x27;s random device. PRNGs could become obsolete but &quot;truly random&quot; will always mean the same. However it&#x27;s not trivial that one has access to a truly random source and in that case it should fall back to fail. At least it could kill the practice of seeding with time.
评论 #9038744 未加载
评论 #9038255 未加载
评论 #9038055 未加载
评论 #9038745 未加载
arsvover 10 years ago
The title is grossly misleading. It should read &quot;C++17 people choose Boost over std::rand() for their RNG needs&quot; or something like that, a hardly surprising statement since C++17 people would choose Boost for pretty much anything else as well.<p>In particular, it has little to do with rand() (as in rand(3) from libc), which has its uses as well as well-known alternatives within C world.<p>As a side note, it&#x27;s funny to see fresh new C++ code that boils down to srand(readintfrom(&quot;&#x2F;dev&#x2F;random&quot;)), except &#x2F;dev&#x2F;random is now given an &quot;abstract standard name&quot; random_device.<p>And that part about limited seeding options. Beats me Boost (the library) alone won&#x27;t fit in the memory of a device with 16bit ints, so inability to seed the RNG will be among the least of their problems.
评论 #9048351 未加载
DonHopkinsover 10 years ago
&quot;(not to be confused with std::mem_fn() – not the dropped ‘u’)&quot; -- note the dropped &#x27;e&#x27;.