- Details
- Written by admin
- Category: PHP
- Published: September 05 2013
- Hits: 1128

Sick and tired of generating pseudo-random numbers using the boring old PHP rand() functions? Well, today we can show you how to generate truly random numbers with Random.org! We are going to use Random.org's HTTP service and PHP's **curl** function to generate random numbers and more.

First off, we will use the Integer Generator to generate simple integers, and we are going to create a function to do so:

<?php function get_random_number($min = 1, $max = 100, $num = 1) { $max = ((int) $max >= 1) ? (int) $max : 100; $min = ((int) $min < $max) ? (int) $min : 1; $options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false, CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => '', CURLOPT_USERAGENT => 'PHP', CURLOPT_AUTOREFERER => true, CURLOPT_CONNECTTIMEOUT => 120, CURLOPT_TIMEOUT => 120, CURLOPT_MAXREDIRS => 10, ); $ch = curl_init('http://www.random.org/integers/?num='. $num .'&min=' . $min . '&max=' . $max . '&col=1&base=10&format=plain&rnd=new'); curl_setopt_array($ch, $options); $content = curl_exec($ch); curl_close($ch); return trim($content); } print get_random_number(); ?>

The first line starts the function, and also holds the variables **$min,** **$max, & $num** which will be the lowest and highest numbers to be called, in this case, the lowest possible number that can be called is 1, and the highest is 100 and also the amount of numbers called with **$num**, in this case its 1**.**

function get_random_number($min = 1, $max = 100, $num = 1) {

The next two lines are checks on the $min & $max variables to make sure they are integers and to make sure $min isnt bigger than $max and they aren't negative numbers.

$max = ((int) $max >= 1) ? (int) $max : 100; $min = ((int) $min < $max) ? (int) $min : 1;

The next section of code is an array that holds the options for curl, most of which has to do with timeouts and header information:

$options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false, CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => '', CURLOPT_USERAGENT => '%MINIFYHTML325d7fba88325a7686d58ab9f6a2284625%%MINIFYHTML325d7fba88325a7686d58ab9f6a2284626%', CURLOPT_AUTOREFERER => true, CURLOPT_CONNECTTIMEOUT => 120, CURLOPT_TIMEOUT => 120, CURLOPT_MAXREDIRS => 10, );

The next line is the initiation of curl and sets the URL to call which also sends the information to Random.org to generate our integers:

$ch = curl_init('http://www.random.org/integers/?num='. $num .'&min=' . $min . '&max=' . $max . '&col=1&base=10&format=plain&rnd=new');

**curl_setopt_array** sets the options for the curl transfer, **curl_exec** executes it, and **curl_close** closes the curl session:

curl_setopt_array($ch, $options); $content = curl_exec($ch); curl_close($ch);

**Trim** removes any whitespaces from the beginning and end of the returned values:

return trim($content);

The final line prints the returned numbers by calling the function and using the **print function**:

print get_random_number();

Please note Random.org's Usage Guidelines, as i have updated the script to do so (minus a quota checker - which i will add later), after erik240 from reddit reminded me that i failed to do so. Source code cownloaded below has not yet been updated.

The demo can be found here.

Source code can be downloaded here.