Sarfraz
Sarfraz

Reputation: 382706

PHP: How to generate biased random numbers

I want to generate random numbers, but these numbers should be somewhat biased, so that numbers in certain ranges appear more frequently than others.

For example, to spread out a series of banner ad impressions in proportion to the number of impressions remaining for each ad campaign.

Upvotes: 0

Views: 714

Answers (1)

Sampson
Sampson

Reputation: 268344

You could generate a random number between 0 and 1, and use it as the percentage of weight for any given banner. If the number is .3 or less, then a less popular (30% of the time) banner will show. If greater, a greater (70% of the time) banner will show.

Upvotes: 2

Related Questions