0

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.

Edited by makimbo: n/a

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by makimbo
0
$rands = range(1,40);
shuffle($rands);

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

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

It works. Thank you so much.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.