Re: rand(0,1) does seem to get more often 1

From: Date: Tue, 07 May 2002 15:45:39 +0000
Subject: Re: rand(0,1) does seem to get more often 1
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-96454@lists.php.net to get a copy of this message
Running the code with such a small number of trials will likely cause skewed results one way or the other. I ran this code and consistently got around the 50-50 split you'd expect: <?php $one = 0; $zero = 0; srand((double) microtime() * 1000000); for ($i = 0; $i < 2000000; $i++) { if (rand(0, 1)) $one++; else $zero++; } print "Total: " . ($one + $zero) . "\n"; print "One: " . $one . "\n"; print "Zero: " . $zero . "\n"; ?> J Andy wrote: > Hi there, > > I am wondering if it is possible that this function tends to be more often > 1. I do get 8 of 10 times 1. > > srand((double)microtime()*1000000); > if (rand(0,1) == 1){ > code... > > it looks ok to me. > > Thanx for any help, > > Andy

« previous php.general (#96454) next »