Loaded dice game should generate a one half of the time

[code]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

loaded dice

loaded dice

loaded dice

Please refresh this page in the browser to roll another die.

[/code] [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]

Check out the comments i have made.
[php]if (rand(0,1)) // this will always be true…
{
$roll = rand(2,6);
}
else // This else will never run because the if will never evaluate to false!
{
$roll = 1;
}[/php]

Try this
[php]
$roll1 = rand(1,6);

// check the roll1 has value of 0 or 1
if($roll1 == 0 || $roll1 == 1) {
$roll = rand(2, 6);
}
else {
$roll = 1;
}[/php]

Red. :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service