## [Solved] use map checksum to seed some random spawning

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

### [Solved] use map checksum to seed some random spawning

I have no idea how to do this. Can someone teach me how to use the map's checksum to seed some predictable random spawning? Say I want to scatter a random bunch of trees all over the map but it uses the map checksum as a seed so that the randomness is, uh, consistent (until the map is altered, I understand).
Last edited by Nash on Thu Feb 22, 2018 1:09 am, edited 1 time in total.

Nash

Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

### Re: [Request] use map checksum to seed some random spawning

If the problem is only in setting the seed then it can be done like this
Code: Select all
`string checksum = level.GetChecksum();int length = checksum.Length(); // should be always 32 for MD5 checksumint seed = 0;for (int i = 0; i < length; ++i){   seed ^= checksum.CharCodeAt(i) << 8 * (i % 4);}SetRandomSeed[my_random_sequence](seed);for (int i = 0; i < 10; ++i){   double x = frandom[my_random_sequence](0, 1000.);   double y = frandom[my_random_sequence](0, 1000.);   console.printf("%f, %f", x, y);}`

Of course you can code more sophisticated string to 32-bit number conversion, for example CRC32.
_mental_

Joined: 07 Aug 2011

### Re: [Request] use map checksum to seed some random spawning

Thank you, this solution works perfectly for the purposes of randomly spawning things based on map checksum.

Now another question: will subsequent random/frandom calls after SetRandomSeed be seeded? I only want SetRandomSeed to affect this one function, and then I want every random/frandom call outside of that to revert to GZDoom's usual, unrpredictable randomness. Do I have to do anything special to achieve this?

Nash

Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

### Re: [Request] use map checksum to seed some random spawning

Only numbers generated with my_random_sequence identifier are affected by seed value. So you need to make sure that name of random sequence identifier is unique.
_mental_

Joined: 07 Aug 2011

### Re: [Solved] use map checksum to seed some random spawning

Alright. Thanks _mental_ for simple and fast solution, :D

Nash