wp_rand

函数
wp_rand ( $min = null, $max = null )
参数
  • (int) $min Optional. Lower limit for the generated number. Accepts positive integers or zero. Defaults to 0.
    Required:
    Default: null
  • (int) $max Optional. Upper limit for the generated number. Accepts positive integers. Defaults to 4294967295.
    Required:
    Default: null
返回值
  • (int) A random non-negative number between min and max.
定义位置
相关方法
wp_headwp_upgradewp_cronwp_readonly_wp_cron
引入
2.6.2
弃用
-

wp_rand: 这是一个WordPress函数,使用内置的PHP函数rand生成一个随机的整数或字符串。它接受两个可选参数,允许你指定随机数的最小和最大值。

生成一个随机的非负数。

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 );
	}
endif;

if ( ! function_exists( 'wp_set_password' ) ) :
	/**
	 * Updates the user's password with a new encrypted one.
	 *
	 * For integration with other applications, this function can be overwritten to
	 * instead use the other package password checking algorithm.
	 *
	 * Please note: This function should be used sparingly and is really only meant for single-time
	 * application. Leveraging this improperly in a plugin or theme could result in an endless loop
	 * of password resets if precautions are not taken to ensure it does not execute on every page load.
	 *
	 * @since 2.5.0
	 *
	 * @global wpdb $wpdb WordPress database abstraction object.
	 *
	 * @param string $password The plaintext new user password.
	 * @param int    $user_id  User ID.
	 */

常见问题

FAQs
查看更多 >