wp_rand()
Generates a random number between the specified min and max.
Pluggable function — this function can be replaced from a plugin. It means that this function is defined (works) only after all plugins are loaded (included), but before this moment this function has not defined. Therefore, you cannot call this and all functions depended on this function directly from a plugin code. They need to be called on plugins_loaded hook or later, for example on init hook.
Function replacement (override) — in must-use or regular plugin you can create a function with the same name, then it will replace this function.
Used By: wp_generate_password()
1 time — 0.000032 sec (very fast) | 50000 times — 0.09 sec (speed of light) | PHP 7.0.8, WP 4.6.1
No Hooks.
Return
Int
. A random non-negative number between min and max.
Usage
wp_rand( $min, $max );
- $min(int)
- Lower limit for the generated number. Accepts positive integers or zero.
- $max(int)
- Upper limit for the generated number. Accepts positive integers.
Default: 4294967295
Examples
#1 Basic usage
echo wp_rand( 99, 99999 ); // 19899, 85724, 77130, 51575 echo wp_rand( 1.5, 5.5 ); // 5, 1, 3, 4 echo wp_rand( 5, 4 ); // always 5 echo wp_rand( 0, 0 ); // 0
Notes
- Global. String. $rnd_value
Changelog
Since 2.6.2 | Introduced. |
Since 4.4.0 | Uses PHP7 random_int() or the random_compat library if available. |
Since 6.1.0 | Returns zero instead of a random number if both $min and $max are zero. |