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.

Evolving the Go Standard Library with math/rand/v2

156 pointsby spaceyabout 1 year ago

7 comments

infogulchabout 1 year ago
&gt; In 2018, Daniel Lemire found an algorithm that avoids the divisions nearly all the time (see also his 2019 blog post). In math&#x2F;rand, adopting Lemire’s algorithm would make Intn(1000) 20-30% faster...<p>I recently found a super simple algorithm that appears to produce a number in the interval [0,N] with a branchless expression with a single multiplication in an extended number size. (Sorry I don&#x27;t have a reference.)<p>Say you want to generate a number, G, in interval [0,N] where N&lt;=UInt32Max. The algorithm is:<p><pre><code> G = uint32( uint64(N)*uint64(rand.UInt32())&gt;&gt;32 ) </code></pre> It seems like this should select a number in the range with no bias. Is there something I missed?
评论 #40232192 未加载
评论 #40231273 未加载
评论 #40232226 未加载
评论 #40239732 未加载
评论 #40233486 未加载
评论 #40243007 未加载
评论 #40255328 未加载
rollulusabout 1 year ago
As often I’m impressed by the quality of all of this. The amount of thinking that went into this, this excellent written blog post. I love the Go blog.
评论 #40233560 未加载
infogulchabout 1 year ago
I like the Principles section. Very measured and practical approach to releasing new stdlib packages. <a href="https:&#x2F;&#x2F;go.dev&#x2F;blog&#x2F;randv2#principles" rel="nofollow">https:&#x2F;&#x2F;go.dev&#x2F;blog&#x2F;randv2#principles</a><p>The end of the post they mention that an encoding&#x2F;json&#x2F;v2 package is in the works: <a href="https:&#x2F;&#x2F;github.com&#x2F;golang&#x2F;go&#x2F;discussions&#x2F;63397">https:&#x2F;&#x2F;github.com&#x2F;golang&#x2F;go&#x2F;discussions&#x2F;63397</a>
评论 #40232605 未加载
Smaug123about 1 year ago
Nice - I wish .NET would be more willing to condemn chunks of the standard library and replace them with something better!
评论 #40229943 未加载
评论 #40231468 未加载
评论 #40232108 未加载
评论 #40232358 未加载
skitterabout 1 year ago
Related, this article discusses difference generators and tradeoffs for Go using them as Source: <a href="https:&#x2F;&#x2F;zephyrtronium.github.io&#x2F;articles&#x2F;randomness.html" rel="nofollow">https:&#x2F;&#x2F;zephyrtronium.github.io&#x2F;articles&#x2F;randomness.html</a>
lifthrasiirabout 1 year ago
&gt; Ideally, the v2 package should be able to do everything the v1 package could do, and when v2 is released, the v1 package should be rewritten to be a thin wrapper around v2.<p>And even more ideally, as many v1 usages should be automatically fixed as possible by `go fix` or similar tools. Allowing this to <i>all</i> user packages would be a major improvement over the status quo.
评论 #40236268 未加载
38about 1 year ago
If both are now crypto secure, what&#x27;s the point of having both? Also seems like they&#x27;ve made math&#x2F;rand slower, not a win in my book.