How can I generate a number not exceeding a specific range?
Example:
I want to generate a number that has 70 digits but doesn't exceed "90000000000000000000000000000000000000000000000000000000000000000000000"
How to do that?
I tried using gmp_init
$range1 = gmp_init("100000000000000000000000000000000000000000000000000000000000000000000000000");
$range2 = gmp_init("900000000000000000000000000000000000000000000000000000000000000000000000000");
$generatedValue = rand($range1,$range2);
echo $generatedValue;
However, it outputs the following error:
PHP Warning: rand() expects parameter 1 to be int, object given
I tried rand(), mt_rand(), random_byte()...
Another Try:
<?php
$rand1 = gmp_random_range(100000000000000000000000000000000000000000000000000000000000000000000000000,900000000000000000000000000000000000000000000000000000000000000000000000000>
echo gmp_strval($rand1) . "\n";
Error: PHP Warning: gmp_random_range(): Unable to convert variable to GMP - wrong type in /home/ubuntu/test/test5.php on line
CodePudding user response:
Let's clear up a few points of confusion:
- PHP's native integers have a maximum of 2 to the power of 63, because they are stored in 64 bits of memory, and the 64th bit is used for negative numbers (I'm simplifying slightly). No matter where you put it in the source code, a larger number than that simply can't exist as a PHP integer, not even briefly.
- GMP is a library which stores numbers a different way, and can represent numbers higher than that. But it can't see what you put in the source code, you have to pass it some other value that PHP can represent, which is why GMP functions accept strings as well as integers.
- Normal PHP functions don't know about GMP - you can't pass a GMP number object to normal mathematical or other functions, you have to use the functions in the GMP library. (There is an exception, which is that you can use operators like and * with GMP objects, and PHP calls the appropriate function for you.)
Bearing all that in mind, we can look at the list of GMP functions in the manual, and find this:
gmp_random_range(GMP|int|string $min, GMP|int|string $max): GMP
Just what we need! Note that it can take arguments of three different types:
- Integers, which are no good in our case, because we can't create integers big enough
- GMP objects, like the ones you created with gmp_init
- Strings, like you used to create the GMP objects
So the simplest way to write your example is:
$rand1 = gmp_random_range('100000000000000000000000000000000000000000000000000000000000000000000000000','900000000000000000000000000000000000000000000000000000000000000000000000000');
This will return a GMP object, so you'll need to look for other GMP functions to decide what to do with it next.
CodePudding user response:
Numbers in php - and most programming languages - has max and min values you can not go beyond.
Maximum integer in php defined in PHP_INT_MAX , Maximum float defined in PHP_FLOAT_MAX.
But if you want a random string larger than this maximum number bounds. You can string concatenation many random numbers to get this random string:
$largeRandomString = mt_rand(10000000, 99999999).mt_rand(10000000, 99999999).mt_rand(10000000, 99999999).mt_rand(10000000, 99999999);