Random Number Generation And Probability

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

Random Number Generation And Probability

Post 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.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Random Number Generation And Probability

Post 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()."')";
 
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

Re: Random Number Generation And Probability

Post by anand »

Thanks :)
Post Reply