php/php-src — pull request #839 by lt (2014-09-24 07:09:28+00:00)
Title: Useful GMP random functions

The existing `gmp_random()` function doesn't allow the range of numbers to be fine-tuned, as the range is determined by a multiple of LIMB_BITS, which varies by platform.

This patch introduces:

- `gmp_random_bits(int bits)` - generates a number between 0 and (2 ** bits) - 1
- `gmp_random_range(mixed min, mixed max)` - generates a number between min and max

Outstanding issues with this patch:

1) I'm not sure how to create deterministic tests for random functions. I have created tests for error conditions, but not sure what more I can do. Suggestions welcome.

2) Attempting to generate a sufficiently large number with `gmp_random_biits()` has two error scenarios. A moderately large number of bits can cause a PHP FATAL because the memory limit is breached, I think this is fine. An insanely large number of bits can cause GMP itself to abort, and I'm not sure how to catch this, although I think it's also fine if it's documented (the same as you can cause PCRE to abort by recursing it out of stack space - it's not PHPs fault)