I am trying to accomplish a loaded dice php program, is there something in the rand I would set up so that it rolls a one half of the time and different numbers the rest of the time. I asked my professor and he told me I was working to far ahead of the class and most likely we would not even have to accomplish this… I want to do it though Thank you … DeAnna. :’(
Depends. Do you want a probability of rolling a 1 half of the time or do you want to enforce that every other roll it will be a 1? If you want to enforce it then you will have to track each roll.
probability of rolling a 1 half of the time… will this be dealing with the rand function?
Yes. Give it a go.
I did give it a go … and its not working correctly[php]<?php
// loaded dice, should roll the number 1 half the time out of a total of 6.
$roll1 = rand(1,6);
if (rand(0,1))
{
$roll = rand(2,6);
}
else
{
$roll = 1;
}
$roll2 = rand(1,6);
print <<<HERE
Rolls normal roll:
You rolled a $roll2.
Rolls the number 1 half the time:
You rolled a $roll1.
HERE;
?>
[/php]
$roll1 and $roll2 are both rand(1,6). The roll that is probably a 1 half the time is called $roll.
[php]$roll1 = mt_rand(0,1) ? mt_rand(1,6) : 1;
$roll2 = mt_rand(1,6);[/php]
so just put that code where I have the code for roll1 and roll2? trying it now…
Here’s a crazy way of doing it… ;D
…and if you want to get really random I’m sure you could do something with this array_rand (php.net Manual). You know this go me thinking I would never trust an online gambling site after doing something like this. :o
[php]<?php
$spinResult = array();
$spinOnes = array();
if (isset($_POST['action']) && $_POST['action'] == 'roll') {
// Number of Rolls:
$numRolls = $_POST['numRolls'];
$count = 1;
$maxFloor = floor($numRolls / 2);
$maxCeil = ceil($numRolls / 2);
while ( $count <= $maxFloor ) {
$dice = rand(2,6);
$spinResult[] = $dice;
$count++;
}
$count = 1;
while ( $count <= $maxCeil) {
$spinOnes[] = 1;
$count++;
}
$spinResult = array_merge($spinResult , $spinOnes);
// Lets' Mix it a little bit....
for ($x = 0 ; $x < 20; $x++) {
shuffle($spinResult);
}
}
?>
Random Number <?php foreach ($spinResult as $key => $spinDisplay) { echo 'Spin Result ' . ($key + 1) . ' is ' . $spinDisplay . '
'; } ?>Number of Rolls:
[/php]