简体   繁体   English

如何在c#中获得受控随机数

[英]how to get controlled random number in c#

I want to generate random numbers but controlled, meaning the numbers should be nearly equally separated, and spread through the range. 我想生成随机数但受控制,这意味着数字应该几乎相等,并在范围内传播。

As an example, if the bounds were 1 and 50, then if the first generated number is 40 then the next number should not be close. 例如,如果边界是1和50,那么如果第一个生成的数字是40,则下一个数字不应该接近。 Suppose it's 20, then 30 would be an acceptable third number. 假设它是20,那么30将是可接受的第三个数字。

Please help. 请帮忙。

Rather than completely random numbers, you might want to look at noise functions like Perlin Noise to generate superficially random data in a predictable fashion. 您可能希望查看像Perlin Noise这样的噪声函数,以可预测的方式生成表面随机数据,而不是完全随机的数字。

http://en.wikipedia.org/wiki/Perlin_noise http://en.wikipedia.org/wiki/Perlin_noise

There are a few variations out there - definitely worth researching if you can describe your segmentation of data algorithmically. 有一些变化 - 如果你能用算法描述你的数据分割,绝对值得研究。

It's used a lot in gaming to smooth and add interest to otherwise randomly generated terrain textures. 它在游戏中用得很多,以平滑并增加对随机生成的地形纹理的兴趣。

There's a few sample implementations in C# out there, this one is used to generate a bitmap but could easily be adapted to fill a 2d array: C#中有一些示例实现,这个实现用于生成位图,但可以很容易地适应填充二维数组:

http://www.gutgames.com/post/Perlin-Noise.aspx http://www.gutgames.com/post/Perlin-Noise.aspx

There's also plenty of questions here on SO about Perlin Noise too: 关于Perlin Noise的问题也有很多问题:

https://stackoverflow.com/search?q=perlin+noise https://stackoverflow.com/search?q=perlin+noise

You may do something like this: 你可能会这样做:

randomSpaced[interval_, mindistance_, lastone_] :=
         (While[Abs[(new = RandomReal[interval])-lastone] < mindistance,];
          Return[new];)  

Randomnicity test drive: 随机性试驾:

For[i = 1, i < 500000, i++,
  rnd[i] = randomSpaced[{0, 40}, 10, rnd[i - 1]];
  ];
Histogram[Table[rnd[i], {i, 500000}]]  

在此输入图像描述

You may see that the frequencies accumulates in the borders 您可能会看到频率在边界中累积

Moreover, if you are not cautious, and ask for a distance too high, the results will be something like: 此外,如果你不谨慎,并要求距离太高,结果将是这样的:

For[i = 1, i < 50000, i++, 
  AppendTo[rnd, randomSpaced[{0, 40}, 25, Last[rnd]]];];
   Histogram[rnd

] ]

在此输入图像描述

because you are not allowing points at the center. 因为你不允许在中心点。

Define a separation distance d the new number should have to the last. 定义新数字应该与最后一个相距的间隔距离。 If the last number was, say, 20 the next random number should not be from 20-d to 20+d. 如果最后一个数字是20,则下一个随机数不应该是20-d到20 + d。 That means the random interval should be [1, 20-d) and (20+d,50]. 这意味着随机区间应为[1,20-d]和(20 + d,50)。

Since you can not call random.next() with two intervals you need to call it with an interval reduced by 2d and then map the random number to your original [1,50] interval. 由于您无法以两个间隔调用random.next(),因此需要以减少2d的间隔调用它,然后将随机数映射到原始[1,50]间隔。

static class RandomExcludingSurrounding
{
    static Random random = new Random();

    public static int Next(int x, int d, int min, int max)
    {
        int next = random.Next(min, max-2*d);
        if (next > x-d)
            next += 2*d;
        return next;
    }
}

int min = 1;
int max = 50;
Random random = new Random();
int next = random.Next(min, max);

while(true)
{
    int next = RandomExcludingSurrounding.Next(next, 20, min, max);
    Console.WriteLine(next);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM