array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'pt_BR', ), 'this' => array ( 0 => 'function.gmp-random-bits.php', 1 => 'gmp_random_bits', ), 'up' => array ( 0 => 'ref.gmp.php', 1 => 'Funções GMP', ), 'prev' => array ( 0 => 'function.gmp-prob-prime.php', 1 => 'gmp_prob_prime', ), 'next' => array ( 0 => 'function.gmp-random-range.php', 1 => 'gmp_random_range', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/gmp/functions/gmp-random-bits.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 5 >= 5.6.3, PHP 7, PHP 8)
gmp_random_bits — Random number
Generate a random number. The number will be between
0
and
2$bits - 1
.
bits
must greater than 0,
and the maximum value is restricted by available memory.
Esta função não gera valores criptograficamente seguros e não deve ser usada para propósitos criptográficos ou fins que exijam que os valores retornados sejam impossíveis de adivinhar.
Se aleatoriedade criptograficamente segura for necessária, a classe Random\Randomizer pode ser usada com o mecanismo Random\Engine\Secure. Para casos de uso simples, as funções random_int() e random_bytes() fornecem uma API conveniente e segura que é garantida pelo CSPRNG do sistema operacional.
bits
The number of bits to generate.
A random GMP number.
If bits
is less than 1
,
a ValueError will be thrown.
Exemplo #1 gmp_random_bits() example
<?php
$rand1 = gmp_random_bits(3); // random number from 0 to 7
$rand2 = gmp_random_bits(5); // random number from 0 to 31
echo gmp_strval($rand1) . "\n";
echo gmp_strval($rand2) . "\n";
?>
O exemplo acima produzirá:
3 15