• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / wp_rand() – Generates a random number.

You appear to be a bot. Output may be restricted

Description

Generates a random number.

Usage

$int = wp_rand( $min, $max );

Parameters

$min
( int ) optional – Lower limit for the generated number
$max
( int ) optional – Upper limit for the generated number

Returns

int A random number between min and max

Source

File name: wordpress/wp-includes/pluggable.php


Lines:

1 to 65 of 65
  function wp_rand( $min = 0, $max = 0 ) {
    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

    // 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 {
        $_max = ( 0 != $max ) ? $max : $max_random_number;
        // 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.
    if ( 0 != $max ) {
      $value = $min + ( $max - $min + 1 ) * $value / ( $max_random_number + 1 );
    }

    return abs( (int) $value );
  }
 

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 9th December 2020

Primary Sidebar

Information

Function name: wp_rand
Plugin ref: WordPress
Version: 5.6
Sourcefile: wp-includes/pluggable.php
File ref: wp-includes/pluggable.php
Deprecated?: No
API Letters: R,W

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites