srand((无符号)(时间(NULL))); (rand())/(RAND_MAX/2) - 1 个 C# 等效项

发布于 2024-07-10 12:34:42 字数 125 浏览 6 评论 0原文

以下 C++ 的 C# 等效项是什么:

srand((unsigned)(time(NULL)));
weight=(double)(rand())/(RAND_MAX/2) - 1;

What is the c# equivalent of the following c++:

srand((unsigned)(time(NULL)));
weight=(double)(rand())/(RAND_MAX/2) - 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒人说梦 2024-07-17 12:34:42

Random 的无参数构造函数使用“依赖于时间的默认种子值”,因此您需要的是:

Random rnd = new Random();
return rnd.Next(-1, 1);

The paramaterless constructor for Random uses "a time-dependent default seed value" so all you need is:

Random rnd = new Random();
return rnd.Next(-1, 1);
度的依靠╰つ 2024-07-17 12:34:42

要在 .NET 中生成随机值,您应该使用 Random 类。 要为其添加时间值,请使用:
随机 rand = new Random((int)DateTime.Now.Ticks);

有关更多细节,最好查看 MSDN 中有关 Random 类的文档,例如哪些方法可用。

To do random value generation in .NET, you should use the Random class. to seed it with a time value, use:
Random rand = new Random((int)DateTime.Now.Ticks);

For further specifics, it's best to check out the docs about the Random class in the MSDN, e.g. which methods are available.

标点 2024-07-17 12:34:42
Random rnd = new Random((int)DateTime.Now.Ticks);
return rnd.Next(-1,1);
Random rnd = new Random((int)DateTime.Now.Ticks);
return rnd.Next(-1,1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文