How to get a controlled random number in C #

I want to generate random numbers, but be controlled, that is, the numbers should be almost equally divided and distributed over a range.

As an example, if the boundaries were 1 and 50, then if the first generated number is 40, then the next number should not be close. Suppose 20, then 30 would be an acceptable third number.

Please, help.

+3
source share
3 answers

Instead of full random numbers, you can look at noise functions like Perlin Noise to generate surface random data in a predictable way.

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

- , .

, .

#, , 2d:

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

SO Perlin Noise:

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

+5

- :

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

:

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

enter image description here

,

, , :

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

]

enter image description here

.

+3

Determine the separation distance d, which the new number should have to the last. If the last number was, say, 20, the next random number should not be from 20 to 20 + d. This means that the random interval must be [1, 20-d) and (20 + d, 50].

Since you cannot call random.next () at two intervals, you need to call it at an interval reduced by 2d, and then map the random number to the original interval [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);
}
0
source

Source: https://habr.com/ru/post/1790272/


All Articles