note 80025 modified in function.mt-rand by felipe

From: Date: Tue, 25 Dec 2007 17:31:09 +0000
Subject: note 80025 modified in function.mt-rand by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-133999@lists.php.net to get a copy of this message
mt_rand does not seem to truly generate random numbers. Im not sure exactly which of the following is more accurate as I was applying the following to sock price deviations which do not follow normal distributions (exactly at least). If you group observations in 11 buckets such that each bucket represents 10 percentiles and run mt_rand(0,10) 100 times you would expect a simlar result as if you ran 100 iterations of mt_rand(0,100) where each bucket represents 1 1 percentile range. FYI I think things work slightly better as the range of mt_rand increases. #$data is a numerical (not associative) array of numbers sort($data); $data_size = count($data); #num_groups is the number of buckets $num_groups = min($num_groups,$data_size); $ile_size = $data_size / $num_groups; //following creates the buckets $groups[] = $data[0]; for ($i=1;$i<$num_groups;$i++){ $x = $i * $ile_size; $i1 = floor($x); $i2 = ceil($x); $d1 = $data[$i1]; $d2 = $data[$i2]; $var = 1 - ($x-$i1); $groups[] = ($d1 * $var) + ($d2 * (1-$var)); } $groups[] = $data[$data_size-1]; //RUN The Random number generator $value = array(); for ($j=0;$j<$depth;$j++){ $t = mt_rand($lower_limit,$upper_limit); $value[] = $this->m_groups[$t]; } //The end result is that as the number of groupas increase the accuracy (ie turue randomness seems to increase proportionally more (up to point). This is not the way it should work in nature. --was-- mt_rand does not seem to truly generate random numbers. Im not sure exactly which of the following is more accurate as I was applying the following to sock price deviations which do not follow normal distributions (exactly at least). If you group observations in 11 buckets such that each bucket represents 10 percentiles and run mt_rand(0,10) 100 times you would expect a simlar result as if you ran 100 iterations of mt_rand(0,100) where each bucket represents 1 1 percentile range. FYI I think things work slightly better as the range of mt_rand increases. #$temp_data is a numerical (not associative) array of numbers sort($data); $data_size = count($data); #num_groups is the number of buckets $num_groups = min($num_groups,$data_size); $ile_size = $data_size / $num_groups; //following creates the buckets $groups[] = $temp_data[0]; for ($i=1;$i<$num_groups;$i++){ $x = $i * $ile_size; $i1 = floor($x); $i2 = ceil($x); $d1 = $temp_data[$i1]; $d2 = $temp_data[$i2]; $var = 1 - ($x-$i1); $groups[] = ($d1 * $var) + ($d2 * (1-$var)); } $groups[] = $temp_data[$data_size-1]; //RUN The Random number generator $value = array(); for ($j=0;$j<$depth;$j++){ $t = mt_rand($lower_limit,$upper_limit); $value[] = $this->m_groups[$t]; } //The end result is that as the number of groupas increase the accuracy (ie turue randomness seems to increase proportionally more (up to point). This is not the way it should work in nature. http://php.net/manual/en/function.mt-rand.php

« previous php.notes (#133999) next »