Is continuously seeding a random number generator performance intensive?

Chris Cain clcain at uncg.edu
Thu Jan 2 17:43:08 PST 2014


On Thursday, 2 January 2014 at 22:01:01 UTC, monarch_dodra wrote:
> *This* comment is confusing me. What do you mean by "re-seed"? 
> You mean a random seed? Once seeded, you shouldn't have to 
> re-seed a PRNG: It'll generate random numbers forever. Or do 
> you mean "re-seed" in the sense "reset"? Because if you do 
> that, then you'll have the same numbers for each coordinates...?

And...

On Friday, 3 January 2014 at 01:01:21 UTC, Frustrated wrote:
> If you re-seed the generator every time you are not doing 
> anything but wasting cycles since the new element will be 
> random, but the same as using the next element in the sequence 
> in the first case.
>
> ...snip..
>
> The nice thing about the first case is that you can save the 
> seed once time and produce the exact same sequence... which 
> would save you memory. In the second case you would have to 
> record every seed to recover the sequence.

I think you both misunderstand what he wants to do.

He's generating a 2D noise map, apparently of arbitrarily large 
size. Let's say his 2D map was 4 billion x 4 billion elements 
long and each element needs to have a range of 0-255 (so, 1 
byte). Obviously storing such a large noise map in memory is not 
feasible.

But if you were able to instead take an x and y coordinate and 
regenerate the information when it becomes necessary, then 
storing all of those positions would be unnecessary. Instead of 
storing 16,000,000,000,000,000,000 bytes (completely infeasible), 
you could store the bounds of map (8 bytes) and generate the part 
of the map you need at any one moment in time (which may need 
perhaps a ~2000x2000 portion or 4MB at a time, depending on what 
he's doing).

So, it sounds like the OP is using the x and y coords for a seed 
to generate a single number and he was curious to whether it 
costs too much to reseed like this for every point.

To the OP:

FWIW, I'm under the impression that this is a fairly common 
strategy, but usually when I've seen this used more than one 
number is generated at a time. You can still do this, in this 
case. For example, divide x by 10 and generate 10 elements 
(column wise) in the noise map each time and it reduces the 
number of reseeds by a factor of 10. Some effort might be wasted, 
but as long as you need a decent chunk of the noise map at any 
one point in time, this should work out pretty well in practice.

My biggest concern with this approach is that you must take care 
that your usage of seeding with x and y coordinates does not 
cause repeated elements to occur. For instance, using `Random rng 
= Random(x + y);` will _not_ work properly (unless you want 
strange mirroring down the diagonal in your noise map). There's 
numerous landmines to avoid when doing something like this. Some 
approaches may not be perfect, but depending on what you're doing 
they may be sufficient, however.


More information about the Digitalmars-d-learn mailing list