PHP选择随机但变量具有不同的权重[重复]

问题描述:

This question already has an answer here:

I'm working on a simple advertisement script, I have a database with all the ads and a weight.

The ad is a picture and the weight is a number from 1 to 20 depending on it's importance (1 shows up rarely and 20 shows up a lot.

But if I'm getting this information from my database how do I sort and choose an ad to display? So can I put a weight on a variabele in a creative way? It's possible to make and array for each weight point and then select a random ad from that array but is there a better alternative?

</div>

此问题已经存在 这里有一个答案: p>

  • MySQL:选择随机输入,但是对某些条目的权重 10个答案 span> li> ul> div>

    我正在制作一个简单的广告脚本,我 有一个包含所有广告和权重的数据库。 p>

    广告是一张图片,重量是从1到20的数字,具体取决于它的重要性(1显示很少,20显示 很多。 p>

    但如果我从我的数据库中获取这些信息,我如何排序和选择要展示的广告?那么我能否以创造性的方式对变量进行加权? 每个重量po都可以制作和排列 int然后从该数组中选择一个随机广告但是有更好的选择吗? p> div>

@Dagon is correct. Use the example from that other question. Again Dagon found it here: MySQL: Select Random Entry, but Weight Towards Certain Entries The comments are really helpful. You basically multiple that row by its weight. To add to that question and answers I would add the rows into an array and then randomly pick one (one array value) to display. When something has more weight just place it in that array times its weight.