I have this function:

function card_draw ($numeroDeobjetos) {
for ($i=0; $i<$numeroDeobjetos; $i++) {
$aleatorio=rand (1,40);
$usada[$aleatorio]=true;
}

for ($i=0; $i<=39; $i++) {
if ($usada[$i]==false) {
echo '<img src="card.jpg">';
}
else {
echo '<img src="'.$i.'.jpg">';
}
}
}

How can I do to prevent repeated numbers in $aleatorio?

Thank you.

$rands = range(1,40);
shuffle($rands);

for ($i=0; $i<$numeroDeobjetos; $i++) {
  $usada[$rands[$i]] = true;
}
$rands = range(1,40);
shuffle($rands);

for ($i=0; $i<$numeroDeobjetos; $i++) {
  $usada[$rands[$i]] = true;
}

It works. Thank you so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.