TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Evolving the Go Standard Library with math/rand/v2

156 点作者 spacey大约 1 年前

7 条评论

infogulch大约 1 年前
&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 未加载
rollulus大约 1 年前
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 未加载
infogulch大约 1 年前
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 未加载
Smaug123大约 1 年前
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 未加载
skitter大约 1 年前
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>
lifthrasiir大约 1 年前
&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 未加载
38大约 1 年前
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.