Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

314
Views
How to use rand() for big numbers

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

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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);
over 4 years ago · Santiago Trujillo Report

0

This solution generates a string with a fixed length of 70 characters and evenly spaced digits from 0 to 9. Only basic PHP functions are required. The algorithm must be modified for 32-bit systems.

$min = "1000000000000000000000000000000000000000000000000000000000000000000000";  
$max = '9000000000000000000000000000000000000000000000000000000000000000000000';
do{
  for($str = "", $i=0; $i < 7; $i++){
    $str .= sprintf("%010d",mt_rand(0,9999999999));
  }
}while($str < $min OR $str > $max);

var_dump($str);
//string(70) "8225821562698285549247617814952633507395510882337701683734245940552330"

The range filter is based on a simple string comparison. For this, $min and $max must contain exactly 70 characters.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!