Is there a way to seed the generators?
giorgiosironi opened this issue · 2 comments
I am interested in using this library as an option for random test input in Eris, but I was wondering if is possible to seed the generators to get a reproducible test run. This option may depend on the source, as I don't think /dev/urandom
is seedable at all while mt_rand()
is, albeit by using global state.
Thank you for any pointers.
No, there is no way to see the generators, and there shouldn't be.
If you need something for testing, mock the generator. If you need a predictable sequence, you can build a single source that's predictable using a hash function to mutate its state.
final class Sequence implements \RandomLib\Source {
protected $seed;
public function __construct($seed) {
$this->seed = hash('sha512', $seed, true);
}
public function generate($size) {
$result = '';
while ($size < strlen($result)) {
$result .= substr($this->seed = hash('sha512', $this->seed, true), 0, 16);
}
return substr($result, 0, $size);
}
}
THIS IS NOT SECURE so only use this for non-sensitive sequences
Not that this is the intended usage of this library, but my use case is generating random input for tests while being able to repeat a failed test to check its resolution. Thanks for the clarification, will look elsewhere.