wp_rand

Definition:
function wp_rand( $min = 0, $max = 0 ) {}

Generates a random number

Parameters

  • int $min: Lower limit for the generated number (optional, default is 0)
  • int $max: Upper limit for the generated number (optional, default is 4294967295)

Return values

returns:A random number between min and max

Source code

function wp_rand( $min = 0, $max = 0 ) {

	global $rnd_value;



	// 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' ) )

			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

	// 4294967295 = 0xffffffff = max random number

	if ( $max != 0 )

		$value = $min + (($max - $min + 1) * ($value / (4294967295 + 1)));



	return abs(intval($value));

}

4011

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: