函数文档

wp_rand()

💡 云策文档标注

概述

wp_rand() 是 WordPress 核心函数,用于生成一个非负随机整数。它支持指定最小值和最大值参数,并优先使用 PHP 的 CSPRNG 方法以确保安全性。

关键要点

  • 函数返回一个介于 $min 和 $max 之间的随机非负整数,参数可选,默认 $min 为 0,$max 为 4294967295。
  • 内部实现优先尝试使用 random_int() 函数(PHP 7+ 或 random_compat 库),失败时回退到传统随机数生成方法。
  • 从 WordPress 6.1 版本开始,当 $min 和 $max 均为 0 时,函数固定返回 0,而非随机数。
  • 函数处理整数参数,浮点数会被转换为整数值,且参数顺序可互换(内部自动调整)。

代码示例

// 生成 1 到 100 之间的随机数(包含边界)
$random_number = wp_rand( 1, 100 );

// 生成一个唯一字符串,附带随机数
$minval = 113241;
$maxVal = 999999;
$random_number = wp_rand( $minval, $maxVal );
$wpdocs_random_id = 'wpdocs-' . $random_number;

注意事项

  • 在 32 位环境中,函数可能处理大整数时存在限制,需注意兼容性问题。
  • 函数与 absint() 相关,确保返回值是非负整数,常用于生成密码、临时数据等场景。
  • 历史版本行为:WordPress 6.1 之前,wp_rand(0, 0) 会返回随机数而非 0,更新后已修正。

📄 原文内容

Generates a random non-negative number.

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

int A random non-negative number between min and max.

Source

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

Version Description
6.1.0 Returns zero instead of a random number if both $min and $max are zero.
4.4.0 Uses PHP7 random_int() or the random_compat library if available.
2.6.2 Introduced.

User Contributed Notes