View Single Post
Old 08-30-2005, 01:19 PM   #6 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 734
DJMaze is on a distinguished road
rand(0,1) isn't accurate enough.
This is because 50% of the time:
2 rolls = 1x1
4 rolls = 2x1
6 rolls = 3x1

There's no guarantee that if you use rand(0,1) 2 times it returns 1x0 and 1x1
To avoid this you should use a cookie or session.

Inside the cookie/session you count how much times the dice is rolled and how much times it was '1'

like:
PHP Code:
<?php
session_start
();
if (!isset(
$_SESSION['count'])) {
  
$_SESSION['count'] = 0;
  
$_SESSION['one'] = 0;
}
if (
$_SESSION['count']%!= 0) {
    if (
$_SESSION['one']*$_SESSION['count']) {
        
$val 1;
    } else {
        
$val rand(2,6);
    }
} else {
    
$val rand(1,6);
}
++
$_SESSION['count'];
if (
$val == 1) ++$_SESSION['one'];

echo 
$val;
This is the most accurate but it does things like: 15411614411513
DJMaze is offline   Reply With Quote