#include <stdio.h>
#include <stdint.h>
#include <ctime>
#include <vector>
#include <map>

template<typename T>
T *RandomSelect(const std::vector<T*> &itemPtrs);
uint32_t Random(uint32_t range);
uint32_t Random2(uint32_t range);

template<typename T>
T * RandomSelect(const std::vector<T*> &itemPtrs)
{
    if (itemPtrs.empty()){ return NULL; }
    uint32_t factor = ;
    std::vector<uint32_t> totalProbs;
    totalProbs.resize(itemPtrs.size());
    totalProbs[] = itemPtrs[]->Prob() * factor;
    ; i < itemPtrs.size(); ++i)
    {
        totalProbs[i] = totalProbs[i - ] + itemPtrs[i]->Prob() * factor;
    }
    uint32_t randInt = Random(totalProbs[totalProbs.size() - ]);
    ; i < totalProbs.size(); ++i)
    {
        if (randInt < totalProbs[i])
        {
            return itemPtrs[i];
        }
    }
    return NULL;
}

uint32_t Random(uint32_t range)
{
    ){ ; }
    return uint32_t((double)rand() / RAND_MAX * range) % range;
}

uint32_t Random2(uint32_t range)
{
    ){ ; }
    uint32_t rand1 = Random();
    uint32_t rand2 = Random();
    uint32_t rand3 = Random();
    uint32_t rand4 = Random();
    uint32_t num = (rand4 << ) | (rand3 << ) | (rand2 << ) | rand1;
    return uint32_t(double(num) / 0xFFFFFFFF * range) % range;
}

struct Item
{
    int32_t prob;
    Item(int32_t p) :prob(p){}
    int32_t Prob(){ return prob; }
};

int32_t main()
{
    std::map<Item*, int32_t> statistic;
    ;
    Item arrItem[] = { , , , , , , , , , ,  };
    std::vector<Item*> vecProbs;
    float sum = 0.0;
    ; j < ]); ++j)
    {
        sum += arrItem[j].Prob();
        vecProbs.push_back(&arrItem[j]);
    }
    ; i < loop; ++i)
    {
        Item *ret = RandomSelect(vecProbs);
        if (ret)
        {
            //printf("%d\n", ret->Prob());
            statistic[ret] += ;
        }
    }
    printf("statistic size = %u\n", statistic.size());
    double error = 0.0;
    for (const std::pair<Item*, int32_t> &p : statistic)
    {
        printf("%-8d %-8d %-12f %-12f\n", p.first->Prob(), p.second, (double)p.second / loop * 100.0, p.first->Prob() / sum * 100.0);
        error += fabs((double)p.second / loop * 100.0 - p.first->Prob() / sum * 100.0);
    }
    printf("error = %f\n", error);
    getchar();
    ;
}

事实证明,Random2要比Random的随机误差略小一点点,多随机几次还是有好处的.

C++ 从一组数据中按概率选择一个的更多相关文章

  1. C#使用拉依达准则(3σ准则)剔除异常数据(.Net剔除一组数据中的奇异值)

    原文:C#使用拉依达准则(3σ准则)剔除异常数据(.Net剔除一组数据中的奇异值) 1.问题的提出: 电池生产中,遇到一批电池的测量结果数据: 电压值 电池个数 电压值 电池个数 电压值 电池个数 电 ...

  2. 工作 巧遇 sql 查询 一组数据中 最新的一条

    SELECT * FROM rsl a, (SELECT CODE, max(time_key) time_key FROM rsl GROUP BY CODE ) b WHERE a. CODE = ...

  3. 获取mysql一组数据中的第N大的值

    create table tb(name varchar(10),val int,memo varchar(20)) insert into tb values('a', 2, 'a2') inser ...

  4. [SQL]一组数据中Name列相同值的最大Je与最小je的差

    declare @t table(name varchar(),qy varchar(),je int) insert into @t union all union all union all un ...

  5. c语言:从一组数据中选出可以组成三角形并且周长最长的三个数(简单)

    题目如下: 思路分析: 写出完整的程序: /* 问题描述: 有n根棍子,棍子i的长度为ai.想要从中选出3根棍子组成周长尽可能长的三角形.请输 出最大的周长,若无法组成三角形则输出0. */ #inc ...

  6. 【剑指offer】求一组数据中最小的K个数

    题目:输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. *知识点:Java PriorityQueue 调整新插入元素 转自h ...

  7. oracle数据中记录被另一个用户锁住

    原因:PL/SQL里面执行语句执行了很久都没有结果,于是中断执行,于是就直接在上面改字段,在点打钩(记入改变)的时候提示,记录被另一个用户锁住. 解决方法: 第一步:(只是用于查看哪些表被锁住,真正有 ...

  8. 在MySQL中,如何计算一组数据的中位数?

    要得到一组数据的中位数(例如某个地区或某家公司的收入中位数),我们首先要将这一任务细分为3个小任务: 将数据排序,并给每一行数据给出其在所有数据中的排名. 找出中位数的排名数字. 找出中间排名对应的值 ...

  9. 在django中如何从零开始搭建一个mock服务

    mock概念 mock 就是模拟接口返回的一系列数据,用自定义的数据替换接口实际需要返回的数据,通过自定义的数据来实现对下级接口模块的测试.这里分为两类测试:一类是前端对接口的mock,一类是后端单元 ...

随机推荐

  1. jquery返回滚动条顶部

    var $view = $('html,body'),$backTop = $('#backTop'), $backTop.on('click',function(){ $view.animate({ ...

  2. 25条提高iOS App性能的建议和技巧

    这篇文章来自iOS Tutorial Team 成员 Marcelo Fabri, 他是 Movile 的一个iOS开发者. Check out his personal website or fol ...

  3. Loadrunner监控Apache

    一.安装Apache yum -y install httpd 二.配置Apache 1.设置开机启动 chkconfig httpd on 2.开启Apache service httpd star ...

  4. 【Java EE 学习 24 上】【注解详解】

    一.注解 1.所有的注解都是类. 2.所有的注解都是Annotation接口的子类. 接口摘要 Annotation 所有 annotation 类型都要扩展的公共接口. 3.定义方式 public ...

  5. 【leetcode】Two Sum

    题目简述: Given an array of integers, find two numbers such that they add up to a specific target number ...

  6. 【chrome插件】web版微信接入图灵机器人API实现自动回复

    小贱鸡自动回复API已经不可以用了,现在改良接入图灵机器人API 360chrome浏览器团队翻译了部分谷歌插件开发文档 地址:http://open.chrome.360.cn/extension_ ...

  7. linux学习笔记-(1)-安装

    学习的第一部,当然是寻找学习资料,如今的网络如此发达,只要下点功夫,基本上能在网上找到一切自己所需要的东西,而且还是免费滴哟! ---------------------分割线------------ ...

  8. 惠普披甲过VR寒冬,花费巨资开发VR游戏

         2016被称为VR元年,各大公司都在积极推动该领域的研究,其中HTC.0culus.索尼的成绩是最高的,不仅推出了自家研发的头显,而且销量还很可观.惠普在VR领域自然也有所投入,但是并没有比 ...

  9. Linux Kernel Oops异常分析

    1.PowerPC小系统内核异常分析 1.1  异常打印 Unable to handle kernel paging request for data at address 0x36fef31eFa ...

  10. 【BZOJ】3993: [SDOI2015]星际战争

    题意 \(m\)个人\(n\)个物品,第\(i\)个物品生命值为\(A_i\),第\(i\)个人每秒可以减少一个物品\(B_i\)的生命值,给出一个\(m \times n\)的矩阵,如果\(i\)行 ...