How to create a constraint on Random.Next ()?

Let's say that I have an object with 10 properties, all randomly generated from an algorithm based on System.Random, using a predefined seed, for example:

System.Random randomNumber = new System.Random(seed);
int prop0 = randomNumber.Next(x0, y0);
int prop1 = randomNumber.Next(x1, y1);
//...
int prop9 = randomNumber.Next(x9, y9);

Everything is fine until I decide to remove 1 property. Let's say this is the first property in my calls Next(), now all changes to the result due to a shifted sequence of calls.

  • What if I want to save the result for other properties?

  • Am I just creating the first property and discarding it?

  • What is the best way to approach this problem in the future to make it easier to update my property list?

+4
source share
3 answers

, .

- Random . , , f(seed, value), "" seed value, .

, value, , .

, , . x from to 0 3, :

int prop0 = RandomX.Generate(seed, 0x45F6C854, x0, y0);
int prop1 = RandomX.Generate(seed, 0x96F4DC41, x1, y1);
//...
int prop9 = RandomX.Generate(seed, 0xFE840301, x9, y9);

, , . , , .

-

int prop0 = RandomX.Generate(globalSeed, objectSeed, propertySeed, from, to);

3 "" from.. to

Generate :

public static uint bitRotate(uint x)
{
    const int bits = 16;
    return (x << bits) | (x >> (32 - bits));
}

public static UInt32 Generate(int seed1, int seed2, int seed3)
{
    // simple "hashing" algorithm
    UInt32 num = 1;
    for (uint i = 0; i < 16; i++)
    {
        // multiply by prime numbers
        num = num * 119 + (uint)seed1;
        num = bitRotate(num);
        num = num * 541 + (uint)seed2;
        num = bitRotate(num);
        num = num * 809 + (uint)seed3;
        num = bitRotate(num);
        num = num * 673 + (uint)i; // not sure if necessary
        num = bitRotate(num);
    }
    return num;
}

uint from.. to.

+3

, , .

, . , List<int>?

+1

, , , , , . - (, RNG).

( ), . , factory, , .

, , , : . field number of next calls. , imho.

0
source

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


All Articles