开发者

excluding previously randomized integer, and randomize again without it

开发者 https://www.devze.com 2022-12-19 04:22 出处:网络
<?php if (isset($_POST[\'开发者_C百科Roll!\'])) { $sides = $_POST[\'sides\']; $rolled = rand(1,$sides);
<?php
        if (isset($_POST['开发者_C百科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:

$possible = range(1, $_POST['sides']);     // build the possible values
$rolledIndex = rand (0, count($possible)); // get a random index
$rolled = $possible[$rolledIndex];         // get the rolled number

unset($possible[$rolledIndex]);            // and remove the rolled nuber

// now you can simply redo the random:
$rolledIndex = rand (0, count($possible)); // get a random index on the smaller array
$rolled = $possible[$rolledIndex];         // get the 2nd rolled number

However, If you want to have a random order on the dice throws, simply use this:

// generate an array with all values from 1 to the posted value (e.g. 1,2,3,4,5,6)
$possible = range(1, $_POST['sides']);  

// this reorders the array by random (e.g. 4,3,1,5,2,6)
$throwOrder = $possible;
shuffle($throwOrder); 
print_r($throwOrder);

Now you can simply iterate over the $throwOrder array and have a random order of dice throws:

Array (
    0 => 4,
    1 => 3,
    2 => 1,
    3 => 5,
    4 => 2,
    5 => 6
)

Edit To get the desired output from the second method, simply do this:

// get the last index of the array of thrown dices
$lastIndex = count($throwOrder)-1;
// iterate through the array, printing the results
foreach ($throwOrder as $key => $rolled) {
    // check if the current key matches the last key in the array
    if ($key != $lastIndex) {
        // if not, the number has lost
        echo $rolled, " was rolled, it lost.\n";
    } else {
        // we have reached the last element of the array
        echo $rolled, " was the last, it won.\n";
    }
}


I think you need to have array $thrownNumbers, which holds every number previously thrown. And every new throw (OMG... the word), you check if it's in array. If yes, throw again. Simplest possible solution.

Edit: As for keeping the array during moving to another page, you can use:

  • $_SESSION
  • cookies (hm)
  • have serialized array as hidden value in form

There.


You can throw it in the $_SESSION to keep track of it between posts/request or append it to the URL and pass it yourself each time as a URI param.


<?php
if(!isset($_SESSION['numbers']){
 $_SESSION['numbers'] = range(1,6); //same as array(1,2,3,4,5,6)
}
$numbers = $_SESSION['numbers'];

if(count($numbers)>1){ //is more than one number left
 $rand_index = array_rand($numbers); //choose a number from an array, returns the index
 print('you rolled '.$numbers[$rand_index].' loser'); // tell them they lost
 unset($numbers[$rand_index]); //remove that element from the array
 $_SESSION['numbers'] = $numbers; //set new array back to session
}else{
 print(array_pop($numbers).' is the winner!'); //pop the remaining number out of the array and print it with winner notification
}
?>

edit: updated to session usage


It is not a bad idea to add to the URL if you use properly. The good side is that you don't store data in the session, the bad side is that the request is bigger each time. To avoid tampering you can use:

$url = 'numbers.php?throwed='.implode(',',$throwed).'&sign='.sha1(serialize($throwed).'mysecretpassword');

And use this to get check:

$throwed = explode(',',$_GET['throwed']);
$not_tampered = sha1(serialize($throwed).'mysecretpassword') == $_GET['sign'];


Just create an array (or other type of list) of all possible numbers and randomly order it. Then, instead of picking a new number each time just advance through the array until you get to the last but one.

Alternatively, you could just go straight to the last number in the list and that's the winner.


It depends on the situation, but creating an array with all the possible numbers for all the visitors in your web can be a very bad idea, sessions files can grow unnecessarily. If they are like numbers from 1 to 10 I think it would be OK, but if they are numbers from 1 to 1 million creating arrays with those ranges saved in the session is pretty much a bad idea. I think it's better to save the rolled results and generate a new one until it doesn't exist in the array.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号