Hi

I would like to introduce a new method for the new Randomizer class [1]. I would like to have a function that generates a random string for me based on a given alphabet. This function is useful as a building block, as many use cases require generating random strings with a specified list of characters (e.g. random passwords, voucher codes, numeric strings larger than integers) and implementing this in userland requires multiple lines of code for what effectively is a very simple operation. Furthermore the obvious implementation based on ->getInt() is inefficient, as it requires at least one call to the engine per character, whereas a 64 Bit engine could generate randomness for 8 characters at once.

I have opened a Pull-Request on PHP to demonstrate the implementation [2] but I am unsure about the method name and decided for `getBytesFromAlphabet()` but I am open for better suggestions.

During code review Go Kudo requested that this goes through an RFC and Tim Düsterhus requested to handle multiple new methods in bulk. Tim implemented a new method, which returns a random float value [3], since that's non-trivial to do in userland and an equally useful building block like my introduced method.

Since the class into which this function is built is final anyway, the methods can be built in with full backwards compatibility.

I look forward to feedback from you!

[1] https://wiki.php.net/rfc/random_extension_improvement
[2] https://github.com/php/php-src/pull/9664
[3] https://github.com/php/php-src/pull/9679

PS: Since this is my first contribution, someone needs to give me karma to open an RFC. My account name is `josh`.

Cheers

Joshua Rüsweg

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to