Page 1 of 1

Random Number Generation And Probability

Posted: Sat Oct 31, 2009 6:53 am
by anand
Hello, I am working on a function which has to insert data into a SQL table.

we have to supply the function with number of entry needs to be inserted into the database and the work of the function is to insert the entry into SQL in such a way that 30% of the entry have their type(a column in my db) as 'a' , 20% as 'b' , 40% as 'c' and rest 10% as 'd'.

Is there any way i can do this? Do I need to run 4 while loops?

Thanks.

Re: Random Number Generation And Probability

Posted: Sat Oct 31, 2009 8:09 am
by Mark Baker

Code: Select all

 
function getRandomType() {
   $randArray = array('a','a','a','b','b','c','c','c','c','d');
   shuffle($randArray);
   return array_pop($randArray);
}
 
$sql = "INSERT INTO table(type) VALUES ('".getRandomType()."')";
 

Re: Random Number Generation And Probability

Posted: Sat Oct 31, 2009 10:41 am
by anand
Thanks :)