wp_rand

Generates a random number.

Description

(int) wp_rand( (int) $min = 0, (int) $max = 0 ); 

Returns (int)

A random number between min and max

Parameters (2)

0. $min — Optional. (int)
Lower limit for the generated number
1. $max — Optional. (int)
Upper limit for the generated number

Usage

  1. if ( !function_exists( 'wp_rand' ) ) { 
  2. require_once ABSPATH . WPINC . '/pluggable.php'; 
  3.  
  4. // Lower limit for the generated number 
  5. $min = -1; 
  6.  
  7. // Upper limit for the generated number 
  8. $max = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_rand($min, $max); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/pluggable.php  
  1. function wp_rand( $min = 0, $max = 0 ) { 
  2. global $rnd_value; 
  3.  
  4. // Some misconfigured 32bit environments (Entropy PHP, for example) truncate integers larger than PHP_INT_MAX to PHP_INT_MAX rather than overflowing them to floats. 
  5. $max_random_number = 3000000000 === 2147483647 ? (float) "4294967295" : 4294967295; // 4294967295 = 0xffffffff 
  6.  
  7. // We only handle Ints, floats are truncated to their integer value. 
  8. $min = (int) $min; 
  9. $max = (int) $max; 
  10.  
  11. // Use PHP's CSPRNG, or a compatible method 
  12. static $use_random_int_functionality = true; 
  13. if ( $use_random_int_functionality ) { 
  14. try { 
  15. $_max = ( 0 != $max ) ? $max : $max_random_number; 
  16. // wp_rand() can accept arguments in either order, PHP cannot. 
  17. $_max = max( $min, $_max ); 
  18. $_min = min( $min, $_max ); 
  19. $val = random_int( $_min, $_max ); 
  20. if ( false !== $val ) { 
  21. return absint( $val ); 
  22. } else { 
  23. $use_random_int_functionality = false; 
  24. } catch ( Error $e ) { 
  25. $use_random_int_functionality = false; 
  26. } catch ( Exception $e ) { 
  27. $use_random_int_functionality = false; 
  28.  
  29. // Reset $rnd_value after 14 uses 
  30. // 32(md5) + 40(sha1) + 40(sha1) / 8 = 14 random numbers from $rnd_value 
  31. if ( strlen($rnd_value) < 8 ) { 
  32. if ( defined( 'WP_SETUP_CONFIG' ) ) 
  33. static $seed = ''; 
  34. else 
  35. $seed = get_transient('random_seed'); 
  36. $rnd_value = md5( uniqid(microtime() . mt_rand(), true ) . $seed ); 
  37. $rnd_value .= sha1($rnd_value); 
  38. $rnd_value .= sha1($rnd_value . $seed); 
  39. $seed = md5($seed . $rnd_value); 
  40. if ( ! defined( 'WP_SETUP_CONFIG' ) && ! defined( 'WP_INSTALLING' ) ) { 
  41. set_transient( 'random_seed', $seed ); 
  42.  
  43. // Take the first 8 digits for our value 
  44. $value = substr($rnd_value, 0, 8); 
  45.  
  46. // Strip the first eight, leaving the remainder for the next call to wp_rand(). 
  47. $rnd_value = substr($rnd_value, 8); 
  48.  
  49. $value = abs(hexdec($value)); 
  50.  
  51. // Reduce the value to be within the min - max range 
  52. if ( $max != 0 ) 
  53. $value = $min + ( $max - $min + 1 ) * $value / ( $max_random_number + 1 ); 
  54.  
  55. return abs(intval($value));