-
-
Notifications
You must be signed in to change notification settings - Fork 262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RNG.clone().setSeed() does not function as expected #201
Comments
Hmmm. A brief look into the RNG implementation does not show anything suspicious: the It would be nice if you managed to reduce your test case to a minimal reproducible version, ideally without the map generator at all: making multiple clones of the RNG and generating a list of random numbers. Few immediate ideas:
|
In this case, this is node-based, not browser, so browser wouldn't affect anything. I'm not sure if it's related to seeding, but I don't think I would be able to tell if it wasn't, as I'm only using seeds. A simple code repro also does not seem to cause it: const ROT = require('rot-js');
const cloned = ROT.RNG.clone().setSeed(1123123);
const seeded = ROT.RNG.setSeed(1123123);
const arr = [1,2,3,4,5,6,7,8,9,10];
for(let i = 0; i < 10; i++) {
const seedVal = seeded.getItem(arr);
const cloneVal = cloned.getItem(arr);
console.log('base', seedVal, 'clone', cloneVal, 'equal', seedVal === cloneVal);
} All the more confusing, IMO, when all I did was change RNG.clone().setSeed() to RNG.setSeed(). The only other fundamental difference is using JS vs TS, but that really wouldn't matter, I think? I did have some trouble importing it in TS though, as I had to import from Sorry I can't be more helpful. I figured I'd report it in case you had an idea, but if not feel free to close this; it may be some strange quirk that's not worth tracking down. |
To clarify: this small code example shows the issue, or works correctly? I am browser-only at the moment, so cannot verify via node.js.
In the rare case of a JS/JIT bug, a browser-based alternative would show different results when compared to your serverside run. So it would make sense - if possible - to run the code client-side as well for comparison. Generally speaking, to find the bug, we will need to reduce the code down as much as possible. If the snippet above does not exihibit the behavior, it means the reduction was too large and we need to reduce less. |
Note (not sure if relevant): cloning a RNG does not seem to persist the seed. In other words:
But you are not reading the seed, right? |
This code shows that in my specific example, there is not an issue (as the randomness matches for at most 3 getItem calls, then deviates).
I did do a getSeed() as well, and it read my seed back to me. So it was definitely setting the seed correctly.
I believe the repo I linked (solokar-map-generator) would run in the browser if the json it required was inlined (the only other things it does are output some json, which can be skipped). Unfortunately I won't be able to help much more until I finish shipping this feature. If I end up thinking of something else that might help I'll post back though. |
The complete RNG state can be seen by calling |
I see!
Yeah, unfortunately the states are exactly the same, even though the results are different. And I honestly have no idea why at this point, as I can see no compelling reason for this to be broken. Also, I am making sure the seed is a number (despite logs above). Oddly, I've also tried discarding more results to see how many results would be the same, and I think there's just some deep-seated mess I've made. No matter what I do, the first "real" thing the RNG has to do (pick a visual combo) is always the same for the same seed, but everything after differs. So I think there's just some really weird issue on my side. Ultimately, I think it's not a big deal and I'll just not clone the RNG, although my confusion as to why cloning the RNG does this is still there. |
I just encountered this issue myself. I know what the problem was in my case, but I'm not 100% sure if it applies to yours. My problem was that even though I was cloning the global RNG instance and setting a seed, none of the map methods such as I mitigated this by copying state from my cloned instance into the global instance before running any of those methods, then copying the state from the global instance back to my clone right after. RNG.setState(worldRNG.getState())
caves.randomize(0.5)
worldRNG.setState(RNG.getState()) If I wasn't |
Something I noticed today when porting my code from JS to TS, I wanted to use RNG.clone().setSeed() so I wasn't overriding the global RNG instance, and was using a unique one per map generation. I noticed that this causes inconsistent results in some weird ways. For example, my first few RNG picks match, but everything after that seem to go completely askew. A few notes:
pictures for comparison:
Might not mean a lot - but when I don't do a .clone() I always get the first image. When I clone the RNG before attempting to use it, I get some variation on the second one - it always picks the same 2 or 3 results the same, but then the rest is unpredictable.
Is there something with RNG.clone that's not being copied correctly?
The text was updated successfully, but these errors were encountered: