Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate fast pseudorandom data in PHP

Tags:

php

random

I need to generate uncompressible data (therefore pseudorandom), and have been trying with the code below. But it's only producing about 10MB/s of data. I need about 100-200 MB/s. Do you have any advice?

if ($length > 8*1024*1024){ //Default max string length in php.
    while (($length - $bytesGenerated)>8*1024*1024){
        $bytesGenerated = $bytesGenerated + (8*1024*1024);
        print(openssl_random_pseudo_bytes(8*1024*1024));
    }
}
print(openssl_random_pseudo_bytes($length - $bytesGenerated));
like image 712
Robert Foss Avatar asked Aug 30 '10 12:08

Robert Foss


People also ask

How do I generate a random 10 digit number in PHP?

The rand() function generates a random integer. Example tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: As of PHP 7.1, the rand() function has been an alias of the mt_rand() function.

How can I generate 5 random numbers in PHP?

rand() or mt_rand() functions can be used to Generate 5 Digit Random Number in PHP.

How do you generate a non repeating random number in PHP?

php $check = array(); function generateNumber() { global $check; $page_no = mt_rand(1,20); $check[] = $page_no; if (count($check) !=

How do I generate a random 4 digit number in PHP?

Generate 4 Digit Random Number using mt_rand() mt_rand() function is just like rand() function but it is 4 times faster than rand() function. To use mt_rand() function define min and max numbers. The mt_rand() function will return a random integer between min and max numbers (including min and max numbers).


1 Answers

if you are working under linux you could just read from /dev/urandom, its kernels fast pseudorandom generator.

on the other way you could use openssl rand and pipe that into php.

like image 192
damir Avatar answered Sep 24 '22 08:09

damir