<?php
if (isset($_POST['Roll!'])) {
$sides = $_POST['sides'];
$rolled = rand(1,$sides);
echo "$rolled was rolled by the dice, it is now out!";
}
?>
This is the code I currently have. After rolling that number, however, I want it to roll again, but without the previously rolled number, until it has rolled all number except one, which would be the winning number. I have no idea how to go about doing that. Any ideas?
EDIT: I’m sorry, I should have been more clear, thank you all for the help so far, but I also need to echo each number rolled, such as
echo "$rolledArray[0] was rolled, it lost.\n";
echo "$rolledArray[1] was rolled, it lost.\n";
echo "$rolledArray[2] was rolled, it lost.\n";
echo "$rolledArray[3] was rolled, it lost.\n";
echo "$rolledArray[x] was rolled, it lost.\n";
echo "$rolledArray[x] was rolled, it lost.\n";
echo "$rolledArray[50?] was rolled, it lost.";
EDIT AGAIN: Also I only want them to have to click Roll! once, not multiple times until they’ve rolled all the numbers, meaning no need for session, I think, though I could be wrong, most of you are clearly more experienced than me.
Sorry, I should have mentioned that before as well.
To answer your direct question:
You can put all of the possible numbers into an array, and get a random index for that array. Once you have an index, remove the item from the array and redo the random over the smaller array:
However, If you want to have a random order on the dice throws, simply use this:
Now you can simply iterate over the
$throwOrderarray and have a random order of dice throws:Edit To get the desired output from the second method, simply do this: