So I have this script in my map where a random number is generated from 1 to 6, then 5 more after it that must be unique every time. To get them unique, I use while loops to check against all previous numbers until the next is unique. The script works, but it seems it's not truly random.
If I save my game in front of the switch to run it, and then run it, see what the number order is and then kill myself to instantly reload and immediately press the switch again, I get the EXACT same order over and over again so long as I press the switch instantly. If I wait longer, the order changes as it always should.
What's going on here?
Random not always random
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.
Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
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.
Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
-
- Posts: 2119
- Joined: Thu May 02, 2013 1:27 am
- Operating System Version (Optional): Windows 10
- Graphics Processor: nVidia with Vulkan support
- Location: Brazil
Re: Random not always random
The game uses a pseudo-random number generator with a seed generated at game start. (Specifically when you start a new game, I believe) PRNGs generate a "random" number using some math on a number called the state. This state is initially set to a value called a "seed".
If it didn't use a PRNG, demos and multiplayer games would go out of sync immediately. The current state of the RNG is also saved to (and loaded from) save files.
The order changes the longer you wait because other things use the RNG too, so the values your script gets when you press it changes.
(FYI, you literally can't get truly random numbers with a computer unless you use something like atmospheric noise or the radioactive decay of a particle, which is VERY slow and is useless for games due to that.)
If it didn't use a PRNG, demos and multiplayer games would go out of sync immediately. The current state of the RNG is also saved to (and loaded from) save files.
The order changes the longer you wait because other things use the RNG too, so the values your script gets when you press it changes.
(FYI, you literally can't get truly random numbers with a computer unless you use something like atmospheric noise or the radioactive decay of a particle, which is VERY slow and is useless for games due to that.)
-
- Posts: 2254
- Joined: Mon Jan 06, 2014 11:32 pm
Re: Random not always random
Ahh I see. So I'm not crazy after all. Fortunately the script only runs once in normal play and it could run at any time during the level.