如何控制随机选择的概率? [英] How to control the probability of random selection?

查看:68
本文介绍了如何控制随机选择的概率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从用户列表中随机选择一个用户,但是我希望VIP用户被选择的可能性更高,如何实现这种算法?

I want to select a user from a user list randomly, but I want the VIP users have higher probability to be selected, how to implement such a algorithm?

样本数据:

$users = array(
    array('name'=>'user1', 'is_vip'=>false),
    array('name'=>'user2', 'is_vip'=>false),
    array('name'=>'user3', 'is_vip'=>false),
    array('name'=>'user4', 'is_vip'=>false),
    array('name'=>'user5', 'is_vip'=>false),
    array('name'=>'user6', 'is_vip'=>true),
    array('name'=>'user7', 'is_vip'=>false),
    array('name'=>'user8', 'is_vip'=>false),
    array('name'=>'user8', 'is_vip'=>true),
    array('name'=>'user10', 'is_vip'=>true),
    array('name'=>'user11', 'is_vip'=>false),
    array('name'=>'user12', 'is_vip'=>false),
);

推荐答案

您可以通过从离散分布中采样来解决此问题.根据他们是否是VIP来为每个不同的用户分配权重,然后使用加权随机抽样算法随机选择它们,但偏向VIP用户.

You can solve this problem by sampling from a discrete distribution. Assign each of the different users a weight based on whether or not they are a VIP, then use a weighted random sampling algorithm to choose them randomly, but with a bias toward VIP users.

有很多算法可以解决这些问题,其中许多算法都很容易编写.网上有详细写信可用.

There are a bunch of algorithms for this and many of them are fast and easy to code up. There's a detailed write up available online that details many of them.

希望这会有所帮助!

这篇关于如何控制随机选择的概率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆