# percentage based output

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Is there a function in PHP that allows me give the likelihood a
certain piece of text is outputted. Say I want one piece of text to
output to have a 60% chance of occurring and another piece of text to
have 40% of occurring on page load.

Much thanks.

## Re: percentage based output

This just sounds like a probability problem.

The usual solution to this problem is to generate a random number
between 0 and 1, and if the number is under (in your case) .6 then
print whatever should print 60% of the time. If it's over .6, print
whatever should print 40% of the time. I don't think there's some
slick method to do all of that for you.

## Re: percentage based output

Thank you for the super quick reply. I thought there might have been a
better way to do this that I didn't think of. Thanks for confirming my
thoughts.

## Re: percentage based output

On Sun, 30 Nov 2008 07:52:03 -0800 (PST), vwenberg@gmail.com wrote:

You should use mt_rand() to generate a random number within a=20
specified range.  To be safe, I would prefer to generate a number=20
between 0 and 100, in this case, to avoid floating point comparisons.

Also note that since PHP 4.2.0, you don't need to seed the random=20
generator.

--=20
Curtis
\$email = str_replace('sig.invalid', 'gmail.com', \$from);