TextGenerator icon indicating copy to clipboard operation
TextGenerator copied to clipboard

Add ability to do a reproducable random choice

Open loekvangool opened this issue 4 years ago • 2 comments

I needed a function that allowed me to do a random choice, but keep the choice linked to a seed value so that the text would stay constant over subsequent runs (even when the template has changed, only the new parts change, the rest stays the same).

Interested in a PR?

loekvangool avatar Jun 05 '20 21:06 loekvangool

Hi @loekvangool ,

Yes, I think this could be a great improvement for the lib to be able to generate reproducible texts ! Do you have the code architecture in mind ?

I have though about it quiclky but maybe the TextGenerator service could provide a getSeed() method that returns a random seed. The generate() function could accept an optional argument $seed that will be transmitted to the "seedable" text functions (random, prandom and shuffle). These functions could implement a "Seedable" interface and a setSeed() function (provided by a Trait ?) and they should be modified to handle the seed. In the TextGenerator::generate() function, we could call setSeed() with the seed (that can be nul) on the seedable functions before generating the text. What do you think ?

neveldo avatar Jun 08 '20 19:06 neveldo

https://github.com/neveldo/TextGenerator/pull/12 created

loekvangool avatar Jun 11 '20 14:06 loekvangool