C#/XNA 伪随机数创建

发布于 2024-12-08 09:12:30 字数 417 浏览 0 评论 0原文

用于创建随机数的 c#/XNA 过程非常快速且简单,但是,它很可能是我见过的最差的分布随机数生成器。有没有更好的、易于c#/XNA实现的方法?

rand.Next() 只是不适合我的需要。

来自:

static private Random rand = new Random();

我在程序中随机放置对象。有时是 10 个,有时是 200 个。

当调用随机对象时(x 值和 y 值在 2d 平面上都是随机的),它们会分组。生成代码是干净的,并称它们是好的,干净地迭代,并用每个值提取一个新的随机数。但它们的分组情况明显很糟糕,这对于随机数来说毕竟不是很好。我是 C# 的中级技能,我是从 as3 过渡过来的,它似乎可以更好地处理随机性。

我很清楚它们是伪随机的,但在 Windows 系统上的 C# 中,分组是怪诞的。

The c#/XNA process for creating random numbers is pretty quick and easy, however, it is quite possibly the worst distributing random number generator I have ever seen. Is there a better method that is easy to implement for c#/XNA?

rand.Next() just doesn't suit my needs.

from:

static private Random rand = new Random();

I randomly place objects, all over my program. sometimes 10, sometimes 200.

When calling for random objects (the x val and y val are both random on a 2d plane), they group. The generation code is clean and calls them good, iterated cleanly and pulls a new random number with each value. But they group, noticeably bad, which isn't very good with random numbers after all. I'm intermediate skill with c#, I crossed over from as3, which seemed to handle randomness better.

I am well-aware that they are pseudo random, but C# on a windows system, the grouping is grotesque.

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

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

发布评论

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

评论(3

羁〃客ぐ 2024-12-15 09:12:30

您可以使用 System.Security.Cryptography.RandomNumberGenerator 来自XNA?

var rand = RandomNumberGenerator.Create();
byte[] bytes = new byte[4];

rand.GetBytes(bytes);

int next = BitConverter.ToInt32(bytes, 0);

要获取最小/最大范围内的值:

static RandomNumberGenerator _rand = RandomNumberGenerator.Create();

static int RandomNext(int min, int max)
{
    if (min > max) throw new ArgumentOutOfRangeException("min");

    byte[] bytes = new byte[4]; 

    _rand.GetBytes(bytes);

    uint next = BitConverter.ToUInt32(bytes, 0);

    int range = max - min;

    return (int)((next % range) + min);
}

Can you use System.Security.Cryptography.RandomNumberGenerator from XNA?

var rand = RandomNumberGenerator.Create();
byte[] bytes = new byte[4];

rand.GetBytes(bytes);

int next = BitConverter.ToInt32(bytes, 0);

To get a value within a min/max range:

static RandomNumberGenerator _rand = RandomNumberGenerator.Create();

static int RandomNext(int min, int max)
{
    if (min > max) throw new ArgumentOutOfRangeException("min");

    byte[] bytes = new byte[4]; 

    _rand.GetBytes(bytes);

    uint next = BitConverter.ToUInt32(bytes, 0);

    int range = max - min;

    return (int)((next % range) + min);
}
悲凉≈ 2024-12-15 09:12:30

对于我的项目,我使用简单的异或算法。我不知道它分配数字有多好,但它很容易实现:

http:// /www.codeproject.com/KB/cs/fastrandom.aspx

For my project I use simple XOR algorythm. I don't know how good it distributes numbers, but it is easy to implement:

http://www.codeproject.com/KB/cs/fastrandom.aspx

蝶舞 2024-12-15 09:12:30

它还可能取决于您使用 System.Random 的方式。作为一般规则,反复创建 Random 的新实例并不是一个好主意,因为大约在同一时间创建的 Random 对象很可能会被播种为相同的随机数(默认情况下,时间用作种子。)相反,创建 Random 的单个实例并在整个程序中仅使用该实例。

除非目标是安全性(这里可能不是这种情况),否则最好使用 System.Random。

It may also depend on how you're using System.Random. As a general rule, it is not a good idea to create new instances of Random over and over, as Random objects created at about the same time will very likely be seeded with the same random number (by default, time is used as the seed.) Instead, create a single instance of Random and use just that instance throughout your program.

Unless the goal is security, which is probably not the case here, you will be better off just using System.Random.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文