Posted By

eortiz on 08/08/12


Tagged

numbers random


Versions (?)

Random numbers from random.org


 / Published in: PHP
 

Random function, thanks to (http://boallen.com/php-get-true-random-number.html "Bo Allen")

  1. <?php
  2. /**
  3.  * Returns a true random number from RANDOM.ORG's integer
  4.  * http interface. Requires cURL.
  5.  *
  6.  * @author Bo Allen
  7.  * @param int $min (Optional) Minimum number (default 1)
  8.  * @param int $max (Optional) Maximum number (default 100)
  9.  * @return mixed Random number (int) on success,
  10.  * error or message (string) on failure
  11.  */
  12. function get_true_random_number($min = 1, $max = 100) {
  13. // Validate parameters
  14. $max = ((int) $max >= 1) ? (int) $max : 100;
  15. $min = ((int) $min < $max) ? (int) $min : 1;
  16. // Curl options
  17. $options = array(
  18. CURLOPT_RETURNTRANSFER => true,
  19. CURLOPT_HEADER => false,
  20. CURLOPT_FOLLOWLOCATION => true,
  21. CURLOPT_ENCODING => '',
  22. CURLOPT_USERAGENT => 'PHP',
  23. CURLOPT_AUTOREFERER => true,
  24. CURLOPT_CONNECTTIMEOUT => 120,
  25. CURLOPT_TIMEOUT => 120,
  26. CURLOPT_MAXREDIRS => 10,
  27. );
  28. // Curl init & run
  29. $ch = curl_init('http://www.random.org/integers/?num=1&min='
  30. . $min . '&max=' . $max . '&col=1&base=10&format=plain&rnd=new');
  31. curl_setopt_array($ch, $options);
  32. $content = curl_exec($ch);
  33. curl_close($ch);
  34. return trim($content);
  35. }

Report this snippet  

You need to login to post a comment.