wp_rand( int $min = null, int $max = null ): int
- Since
- 2.6.2, 4.4.0, 6.1.0
- Source
wp-includes/pluggable.php:3009
Compatibility
- WordPress
- since 6.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$minintoptional- Lower limit for the generated number.
Accepts positive integers or zero. Defaults to 0.Default:null $maxintoptional- Upper limit for the generated number.
Accepts positive integers. Defaults to 4294967295.Default:null
Return value
int- A random non-negative number between min and max.
Performance profile
How much work a call to wp_rand() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Moderate
- Scaling
- Constant
- Instructions
- 29–98
- Plugin surface
- None
- Called by
- 8
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 109.
Nothing here hands control to plugin code.
8 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- transienttransient
get_transient()called directly - hookthird-party callbacks
apply_filters()one call below wp_rand() - cacheobject cache
wp_cache_get()one call below wp_rand() - optionoption read or write
get_option()one call below wp_rand()
Further down the call graph this can also reach serialize and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 11 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_rand() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$val !== false | 29–31 | random_int(), absint() |
| always | 41–43 | hexdec(), abs(), abs() |
$val === false | 54–56 | random_int(), hexdec(), abs(), abs() |
defined('WP_SETUP_CONFIG') | 75–79 | microtime(), mt_rand(), uniqid(), md5(), sha1(), sha1(), md5(), hexdec(), abs(), abs() |
!defined('WP_SETUP_CONFIG') | 77–81 | get_transient(), microtime(), mt_rand(), uniqid(), md5(), sha1(), sha1(), md5(), hexdec(), abs(), abs() |
!defined('WP_INSTALLING') | 81–83 | microtime(), mt_rand(), uniqid(), md5(), sha1(), sha1(), md5(), set_transient(), hexdec(), abs(), abs() |
!defined('WP_SETUP_CONFIG') && !defined('WP_INSTALLING') | 83–85 | get_transient(), microtime(), mt_rand(), uniqid(), md5(), sha1(), sha1(), md5(), set_transient(), hexdec(), abs(), abs() |
$val === false && defined('WP_SETUP_CONFIG') | 88–92 | random_int(), microtime(), mt_rand(), uniqid(), md5(), sha1(), sha1(), md5(), hexdec(), abs(), abs() |
$val === false && !defined('WP_SETUP_CONFIG') | 90–94 | random_int(), get_transient(), microtime(), mt_rand(), uniqid(), md5(), sha1(), sha1(), md5(), hexdec(), abs(), abs() |
$val === false && !defined('WP_INSTALLING') | 94–96 | random_int(), microtime(), mt_rand(), uniqid(), md5(), sha1(), sha1(), md5(), set_transient(), hexdec(), abs(), abs() |
$val === false && !defined('WP_SETUP_CONFIG') && !defined('WP_INSTALLING') | 96–98 | random_int(), get_transient(), microtime(), mt_rand(), uniqid(), md5(), sha1(), sha1(), md5(), set_transient(), hexdec(), abs(), abs() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 109 | 29–98 | 8 | |
| 8.5 | 109 | 29–98 | 8 | |
| 8.4 | 109 | 29–98 | 8 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 121 | 35–110 | 8 | |
| 8.2 | 121 | 35–110 | 8 | |
| 8.1 | 121 | 35–110 | 8 | |
| 7.4 | 121 | 35–110 | 8 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 3
- absint()Converts a value to non-negative integer.
- get_transient()Retrieves the value of a transient.
- set_transient()Sets/updates the value of a transient.
Used by · 8
- get_the_password_form()Retrieves protected post password form content.
- print_embed_sharing_dialog()Prints the necessary markup for the embed sharing dialog.
- send_confirmation_on_profile_email()Sends a confirmation request email when a change of user email address is attempted.
- twenty_twenty_one_password_form()Retrieves protected post password form content.
- update_option_new_admin_email()Sends a confirmation request email when a change of site admin email address is attempted.
- wp_generate_password()Generates a random password drawn from the defined set of characters.
- wpmu_signup_blog()Records site signup information for future activation.
- wpmu_signup_user()Records user signup information for future activation.
Source code
function wp_rand( $min = null, $max = null ) { global $rnd_value; /* * Some misconfigured 32-bit environments (Entropy PHP, for example) * truncate integers larger than PHP_INT_MAX to PHP_INT_MAX rather than overflowing them to floats. */ $max_random_number = 3000000000 === 2147483647 ? (float) '4294967295' : 4294967295; // 4294967295 = 0xffffffff if ( null === $min ) { $min = 0; } if ( null === $max ) { $max = $max_random_number; } // We only handle ints, floats are truncated to their integer value. $min = (int) $min; $max = (int) $max; // Use PHP's CSPRNG, or a compatible method. static $use_random_int_functionality = true; if ( $use_random_int_functionality ) { try { // wp_rand() can accept arguments in either order, PHP cannot. $_max = max( $min, $max ); $_min = min( $min, $max ); $val = random_int( $_min, $_max ); if ( false !== $val ) { return absint( $val ); } else { $use_random_int_functionality = false; } } catch ( Error $e ) { $use_random_int_functionality = false; } catch ( Exception $e ) { $use_random_int_functionality = false; } } /* * Reset $rnd_value after 14 uses. * 32 (md5) + 40 (sha1) + 40 (sha1) / 8 = 14 random numbers from $rnd_value. */ if ( strlen( $rnd_value ) < 8 ) { if ( defined( 'WP_SETUP_CONFIG' ) ) { static $seed = ''; } else { $seed = get_transient( 'random_seed' ); } $rnd_value = md5( uniqid( microtime() . mt_rand(), true ) . $seed ); $rnd_value .= sha1( $rnd_value ); $rnd_value .= sha1( $rnd_value . $seed ); $seed = md5( $seed . $rnd_value ); if ( ! defined( 'WP_SETUP_CONFIG' ) && ! defined( 'WP_INSTALLING' ) ) { set_transient( 'random_seed', $seed ); } } // Take the first 8 digits for our value. $value = substr( $rnd_value, 0, 8 ); // Strip the first eight, leaving the remainder for the next call to wp_rand(). $rnd_value = substr( $rnd_value, 8 ); $value = abs( hexdec( $value ) ); // Reduce the value to be within the min - max range. $value = $min + ( $max - $min + 1 ) * $value / ( $max_random_number + 1 ); return abs( (int) $value ); }Changelog
Introduced in 2.6.2. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$min and $max are zero.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/pluggable.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.