0

I recently created a RNG LCG, the formula is:

newSeed = (131173 * actualSeed + 27) % 262144;

But I have a question, this RNG is used to introduce a single seed and then it will generate a sequence. But I, for other reasons, need to introduce a seed for each number that is governed by: n * x, n is any number and x is the x coordinate, with this I achieve that if n is a seed and I want to generate a random map , will always generate the same.

The problem is that doing this is the equivalent of:


y = (131173 * x + 27) % 262144

and this gives numbers that grow and return to zero when 131173 * x + 27 is equal to 262144(m), this does not give the illusion of being random, I have tried that when introducing the seed, the seed is modified so that the resulting numbers are with an aspect more random, but I don't know if this works well, I need advice.

I have tried that when the seed is entered, the seed is:

x * x

, and after

(x * x) | n(seed)

, this does give me a more random look, but I don't know if it's the best way to solve my problem.

cafce25
  • 15,907
  • 4
  • 25
  • 31
  • 2
    "this does not give the illusion of being random". No, of course it doesn't. It's a LCG, with very little state. That's never going to be good. You can spend an enormous amount to make it slightly less worse. But the question tells me that you're not familiar with PRNG theory, so the best advice would be to get a proper off-the-shelf implementation such as the Mersenne Twister. – MSalters Feb 28 '23 at 10:28
  • 1
    Please [edit] your question and explain how you checked that "this gives numbers that grow and return to zero". When I use this calculation, the resulting numbers look somewhat random. Of course it may calculate 0 at some point. Show the different versions of your code instead of describing it. – Bodo Feb 28 '23 at 10:36
  • You cannot even begin to think about the theory behind this all without stating the types of all variables involved. Specifically `actualSeed`. Is it a 32 bit int system? – Lundin Feb 28 '23 at 11:00
  • Note that `(131173 * actualSeed + 27) % 262144` is like `(131173 * actualSeed + 27) >> 18;`. That is throwing away a lot of bits each iteration. I expect `newSeed` to have only 1 of 16k values. I am confident this does not work well, yet I find OP's overall goal unclear. Marcos Barceló Pérez, please add detail to your goal. – chux - Reinstate Monica Feb 28 '23 at 13:30
  • If you are going to use your own LCG, use a modulus much bigger than the number of numbers you need in your pool. E.g., if you want to select numbers from 0 to 127, then a modulus of 262,144 may be fine for many purposes. (You should use the high bits of the underlying sequence to generate the numbers 0 to 127. The low bits form a subset LCG. The high bits exhibit more “randomness.”) If you want to select numbers from 0 to 262,143, then an LCG with a modulus of 262,144 will step through a single sequence. – Eric Postpischil Feb 28 '23 at 15:46
  • If you do not need to use your own LCG, the system you are using likely has better random number generators available, perhaps `random` instead of `rand`. – Eric Postpischil Feb 28 '23 at 15:47

1 Answers1

0

The usual start for PRNG seed is using a time reference so that you will know that on the next round, it will start from a new seed.

A classic for the standard library srand is in fact

srand (time(NULL));
LuC
  • 347
  • 2
  • 10