Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: Add a method to get the current state of Random\Engine\PcgOneseq128XslRr64 engine #17937

Closed
Crovitche-1623 opened this issue Feb 26, 2025 · 5 comments
Labels

Comments

@Crovitche-1623
Copy link

Crovitche-1623 commented Feb 26, 2025

Description

Actually a jump method is available to moves the algorithm’s state ahead by the number of steps given by advance. However, there are no built-in method to retrieve the current state of the algorithm.

This force to keep a class that decorates this engine to save the state of the algorithm although the state is known internally (I imagine) if we want to restore the state later (e.g. when creating a new engine).

It would be a good improvement to have a getState() method that returns the number of steps the algorithm has already advanced.

@Crovitche-1623
Copy link
Author

E.g. Python has a method to do this: https://docs.python.org/3/library/random.html#random.getstate

@TimWolla
Copy link
Member

TimWolla commented Feb 28, 2025

All the engines (except Secure) support serialization:

<?php

var_dump(serialize(new \Random\Engine\PcgOneseq128XslRr64(0)));

results in:

string(121) "O:33:"Random\Engine\PcgOneseq128XslRr64":2:{i:0;a:0:{}i:1;a:2:{i:0;s:16:"f8734299c9be7148";i:1;s:16:"9a4583381c8a1fac";}}"

@Crovitche-1623
Copy link
Author

Crovitche-1623 commented Mar 3, 2025

Thanks @TimWolla.

In other words if the engine needs to be restored later, you think it's more reliable to store the serialized engine and retrieve it later using deserialization than storing the state property? The only drawback I can see is that it would be less interoperable with other systems if only the state (with its seed in certain cases too) is needed to rebuild the engine.

@TimWolla
Copy link
Member

TimWolla commented Mar 3, 2025

Serialization is the intended solution to serialize the engine state, yes.

Interoperability is not really a thing either way, since the functions to turn the engine output into, say, an integer (Randomizer::getInt()) will differ between different programming languages. The raw engine output is not all that interesting by itself.

@Crovitche-1623
Copy link
Author

Okay, the result won't be the same if the implementation isn't exactly the same from what I understand. That makes sense now thanks for your help.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants