random_int

(PHP 7)

random_intGenerates cryptographically secure pseudo-random integers

Descrição

random_int ( int $min , int $max ) : int

Generates cryptographic random integers that are suitable for use where unbiased results are critical, such as when shuffling a deck of cards for a poker game.

As fontes de aleatoriedade dessa função são:

  • No Windows, a função » CryptGenRandom() sempre será utilizada.
  • No Linux, a syscall » getrandom(2) será utilizada se disponível.
  • Em outras plataformas /dev/urandom será utilizada.
  • Se nenhuma das fontes acima estiver disponível, então uma Exception será lançada.

Nota: Embora essa função tenha sido adicionado no PHP 7.0, existe » implementação userland disponível para PHP 5.2 ao 5.6.

Parâmetros

min

The lowest value to be returned, which must be PHP_INT_MIN or higher.

max

The highest value to be returned, which must be less than or equal to PHP_INT_MAX.

Valor Retornado

Returns a cryptographically secure random integer in the range min to max, inclusive.

Erros

  • Se uma fonte de aleatoriedade não for encontrada uma Exception será lançada.
  • No caso de parâmetros inválidos um TypeError será lançado.
  • If max is less than min, an Error will be thrown.

Exemplos

Exemplo #1 random_int() example

<?php
var_dump
(random_int(100999));
var_dump(random_int(-10000));
?>

O exemplo acima irá imprimir algo similar à:

int(248)
int(-898)

Veja Também

  • random_bytes() - Generates cryptographically secure pseudo-random bytes