For general use the @Leandro response can be used. However for being a card game, although predictability is not something so easy... it is still possible, so my comment.
Even by law in some cases, it is necessary to use a truly random generator, a dedicated hardware, or at least a CSPRNG. On poker websites that are regulated undergo testing, for example Gaming Labs, they have their own tests and specific to test the generator. But, these issues are more bureaucratic, off the subject of Stackoverflow, I just mention here because I don’t believe it’s something "irrelevant".
Requires PHP 7+
The RAND()
Mysql is not a CSPRNG, they themselves do not guarantee it. PHP has its own function which is the random_int
and the random_bytes
who claims to be a CSPRNG, he uses the /dev/urandom
.
He must be more reliable than the RAND()
Mysql, for this you can use:
function gerar(int $quantidade, int $minimo, int $maximo) : array
{
$arr = [];
for ($i = 0; $i < $quantidade; $i++) {
$arr[] = random_int($minimo, $maximo);
}
return $arr;
}
This will generate X numbers using the random_int
, so you can take the data as follows:
$numerosGerados = gerar(50, 0, 299);
$stmt = mysqli_prepare($mysqli, 'SELECT coluna FROM tabela WHERE id IN ('.substr(str_repeat('?,', count($numerosGerados)),0,-1).')');
mysqli_stmt_bind_param($stmt, str_repeat('i', count($numerosGerados)), ...$numerosGerados);
mysqli_stmt_execute($stmt);
$resultado = mysqli_stmt_get_result($stmt);
while($linha = mysqli_fetch_assoc($resultado)){
echo $linha['coluna'];
}
This starts from some premises:
- The table is in order and has a
0
until 299
.
- There may be repeated letters.
The ideal is that you store the generated cards in a new table associated with the player, so you can check if the player really has the card he says he has played, for example.
Recalling that the
RAND()
is not CSPRNG, cryptographically-secure, according to the documentation itself "RAND() is not Meant to be a Perfect Random Generator". I think for the purpose of letters it should not be used in any hypothesis. There are already several cases about this, in the same context of letters, That’s what I just found.– Inkeliz
Do you have any safe suggestions, inkeliz?
– Luizinho
For the application in question Rand() applies.
– Motta
Personal beauty, vlw by tips!
– Luizinho