# wp_rand()

URL: https://chugunov.pro/api-wordpress/functions/wp_rand/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 2.6.2.

## Сигнатура

```php
wp_rand( int $min = null, int $max = null ): int
```

## Описание

Генерирует случайное неотрицательное число.

## Параметры

- `$min` `int` — необязательный, по умолчанию `null`. Нижняя граница генерируемого числа.
  
  Принимает положительные целые числа или ноль. Значение по умолчанию — 0.
- `$max` `int` — необязательный, по умолчанию `null`. Верхняя граница генерируемого числа.
  
  Принимает положительные целые числа. Значение по умолчанию — 4294967295.

## Возвращаемое значение

`int`

## Исходный код

Файл: `wp-includes/pluggable.php:3005`

```php
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 );
}
```

## История изменений

- 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.

## Связанные

Использует: [`get_transient`](https://chugunov.pro/api-wordpress/functions/get_transient/), [`set_transient`](https://chugunov.pro/api-wordpress/functions/set_transient/), [`absint`](https://chugunov.pro/api-wordpress/functions/absint/).
Используется в: [`print_embed_sharing_dialog`](https://chugunov.pro/api-wordpress/functions/print_embed_sharing_dialog/), [`update_option_new_admin_email`](https://chugunov.pro/api-wordpress/functions/update_option_new_admin_email/), [`send_confirmation_on_profile_email`](https://chugunov.pro/api-wordpress/functions/send_confirmation_on_profile_email/), [`wp_generate_password`](https://chugunov.pro/api-wordpress/functions/wp_generate_password/), [`get_the_password_form`](https://chugunov.pro/api-wordpress/functions/get_the_password_form/), [`wpmu_signup_blog`](https://chugunov.pro/api-wordpress/functions/wpmu_signup_blog/), [`wpmu_signup_user`](https://chugunov.pro/api-wordpress/functions/wpmu_signup_user/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/wp_rand/
