STL icon indicating copy to clipboard operation
STL copied to clipboard

`<random>` Pick a different backing random number engine for `default_random_engine`

Open horenmar opened this issue 1 year ago • 5 comments
trafficstars

Currently it is alias for mt19937. mt19937 is not a bad generator on its own, but it is not a good default. The reason is that MTs are excessively large, so sizeof(default_random_engine) == 5000 right now. This is much larger than the user would reasonably expect, for comparison, xoshiro fits into 16-32 bytes, PCG usually fits into 8-16, common LCGs will be 8. WELLs can be as large as MTs, but there are also reasonably sized variants at 32 bytes.

The choice of default_random_engine also has knock-on effects beyond just the direct users, e.g. the simple random utilities wanted to use a thread-local instance of default_random_engine, but that means shoving 5k objects into the TLS. As I understand it, there is nothing really blocking that, but it is not great.

horenmar avatar Dec 12 '23 11:12 horenmar

Thank you for your suggestion! It seems reasonable, but it would require an ABI-breaking change. We can consider implementing it in the vNext version (not Visual Studio 2022).

vNext note: Resolving this issue will require breaking binary compatibility. We won't be able to accept pull requests for this issue until the vNext branch is available. See https://github.com/microsoft/STL/issues/169 for more information.

fsb4000 avatar Dec 13 '23 14:12 fsb4000

изображение

fsb4000 avatar Dec 13 '23 14:12 fsb4000

Yeah I know, I talked with STL somewhere and he said to open an issue so it might happen in vNext.

horenmar avatar Dec 13 '23 15:12 horenmar

LCGs are garbage but if there were a better option than mt19937[_64] then we should consider it for vNext. There appears to be nothing technically preventing default_random_engine from being a choice outside the Standard set, so it could be an implementation of PCG or some other algorithm.

StephanTLavavej avatar Dec 13 '23 22:12 StephanTLavavej