tmedwards / sugarcube-2

SugarCube is a free (gratis and libre) story format for Twine/Twee.
https://www.motoslave.net/sugarcube/2/
BSD 2-Clause "Simplified" License
177 stars 41 forks source link

Seeded PRNG with the either function #88

Closed zen14774 closed 3 years ago

zen14774 commented 3 years ago

I was adding some tests to cover a couple tricky scenes, so I was using State.prng.init to make the tests reproducible but it looks like it's not consistent.

I tracked it to the either function, replaced it with a simple function that uses random and it seems that I have reproducible tests now. Is that function not using the seed? Should I not expect it to be consistent with the other random methods?

I'm not too worried because I have a workaround but I wanted to report in case it's just an oversight.

tmedwards commented 3 years ago

The affected APIs are noted by the second sentence of the documentation for the State.prng.init() API—among other places.

Once initialized, the State.random() method and story functions, random() and randomFloat(), return deterministic results from the seeded PRNG—by default, they return non-deterministic results from Math.random().

Neither either() nor methods like <Array>.random() are affected.

zen14774 commented 3 years ago

Perhaps the point I should have made is that they would be more useful if they also were deterministic when using a seed, like the two that are. But I guess from your reply that there's some reason to it and it's not just that it didn't occur to you to add it. Anyway that's up to you and I have a workaround so I'll forget about it. Thanks for your reply.

greyelf commented 3 years ago

Having both deterministic and non-deterministic methods of generating a random number/selection allows the end-user to choose which type they want to use, assuming that the end-user is aware of what seeding is. If all the story format included 'random' functions were only deterministic then the end-user would need to know how to create their own methods if they wanted non-deterministic ones.

zen14774 commented 3 years ago

Is it a common use case that you want to mix deterministic and non-deterministic? My expectation with a framework that can be seeded is that if I provide a seed the different methods will behave consistently, not that only half the methods will be seeded and I'll have to re-implement the other half to have deterministic behavior that I can test. If I want to mix different behaviors it'd be up to me to manage different seeds or roll my own thing.

That is of course just my opinion. I'm only trying to make my point of view clear here, I get that tmedwards is aware of it and it's intended so I don't expect it to change just because I don't like it.