# 生成带权重的随机数

``````#include
#include
#include
#include
#include

using std::vector;
using std::rand;
using std::srand;
using std::cout;
using std::endl;

class MyMath{
public:
vector GetRandomNumWithWeight(vector weight,int number){
int size = weight.size();
vector res;
int accumulateValue = accumulate(weight.begin(),weight.end(),0);
srand(time(0));// srand()一定要放在循环外面或者是循环调用的外面，否则的话得到的是相同的随机数
for(int i = 0;i < number; i++)
{
int tempSum = 0;
int randomNnm = 0;
randomNnm = rand() % accumulateValue;
//0 ~ weight[0]为1，weight[0]+1 ~ weight[1]为2，依次类推
for(int j = 0;j < size;j++)
{
tempSum += weight[j];
cout << randomNnm << endl;
if(randomNnm <= tempSum)
{
res.push_back(j+1);
break;
}
}
}
return res;
}

};

int main()
{
vector weight = {1000, 2000, 3000, 1000, 1000, 500, 500, 500, 500 };//数字1-9的权重(这里的数字范围与权重都可以自定义)
MyMath myMath;
vector result =  myMath.GetRandomNumWithWeight(weight,5);
for(auto const &num:result)
{
cout << num << ' ';
}
cout << endl;
return 0;
}

``````

Let's start by defining an integer array w_list to store our random weights.
Define w_sum to store the sum of weights.
Int length = sizeof(w_list)/sizeof(int);
Then, a for loop adds up each item of the w_list with w_sum.
Int randVal and store each weight in it. int randVal = rand() % w_sum;
This step can be a little confusing, for example, if there are 100 weights (the total weight is 100), we use rand()%100, and the result is each weight.

``````for (int i = 0; i < length; i++)
{
if (randVal <= w_list[i])
{
rward = i;
break;
}
randVal -= w_list[i];
}``````

``````srand((unsigned)time(NULL));
int w_list[10] = { 		2, 			4, 		 15, 	 15, 	 16 ,  	 16 ,  25 ,    7 , 	5 };
string names[10] = { "宇宙超级凶", "大凶", "中平", "小平", "小凶" ,"中吉","小吉","超级吉","中凶" };
int w_sum = 0;
int length = sizeof(w_list) / sizeof(int);
for (int i = 0; i < length; i++)
{
w_sum += w_list[i];
}
int randVal = rand() % w_sum;
int rward = 0;
for (int i = 0; i < length; i++)
{
if (randVal <= w_list[i])
{
rward = i;
break;
}
randVal -= w_list[i];
}``````

# 代码

``````#include
#include
#include
using namespace std;
int rd(int a,int b){
srand((unsigned)time(NULL));
return (rand()%(b-a+1)+a);
}
int main(){
system("color F0");
srand((unsigned)time(NULL));
int w_list[10] = { 		2, 			4, 		 15, 	 15, 	 16 ,  	 16 ,  25 ,    7 , 	5 };
string names[10] = { "宇宙超级凶", "大凶", "中平", "小平", "小凶" ,"中吉","小吉","超级吉","中凶" };
string yi_list[100][100]={
{"宜:诸事不宜","宜:诸事不宜","宜:诸事不宜","宜:诸事不宜"},
{"宜:装弱","宜:窝在家里","宜:刷题","宜:吃饭"},
{"宜:刷题","宜:开电脑","宜:写作业","宜:睡觉"},
{"宜:发朋友圈","宜:出去玩","宜:打游戏","宜:吃饭"},
{"宜:学习","宜:研究Ruby","宜:研究c#","宜:玩游戏"},
{"宜:膜拜大神","宜:扶老奶奶过马路","宜:玩网游","宜:喝可乐"},
{"宜:吃东西","宜:打sdvx","宜:打开洛谷","宜:出行"},
{"宜:写程序","宜:刷题","宜:偷塔","宜:上CSDN"},
{"宜:扶老奶奶过马路","宜:上课","宜:写作业","宜:写程序"},
};
string yi_shi_list[100][100]={
{"","","",""},
{"谦虚最好了","不出门没有危险","直接AC","吃的饱饱的再学习"},
{"一次AC","发现电脑死机了","全对","睡足了再学习"},
{"点赞量破百","真开心","十连胜","吃饱了"},
{"都会","有了新发现","发现新大陆","直接胜利"},
{"接受神之沐浴","增加RP","犹如神助","真好喝"},
{"吃饱了","今天状态好","发现AC的题变多了","路途顺畅"},
{"不会报错","直接TLE","胜利","发现粉丝涨了200个"},
{"增加RP","听懂了","都会","没有Bug"},
};
string ji_list[100][100]={
{"忌:诸事不宜","忌:诸事不宜","忌:诸事不宜","忌:诸事不宜"},
{"忌:打sdvx","忌:出行","忌:玩手机","忌:吃方便面"},
{"忌:关电脑","忌:开挂","忌:纳财","忌:考试"},
{"忌:膜拜大神","忌:评论","忌:研究Java","忌:吃方便面"},
{"忌:发朋友圈","忌:打开洛谷","忌:研究C++","忌:出行"},
{"忌:探险","忌:发视频","忌:发博客","忌:给别人点赞"},
{"忌:写程序","忌:使用Unity打包exe","忌:装弱","忌:打开CSDN"},
{"忌:点开wx","忌:刷题","忌:打吃鸡","忌:和别人分享你的程序"},
{"忌:纳财","忌:写程序超过500行","忌:断网","忌:检测Bug"},
};
string ji_shi_list[100][100]={
{"","","",""},
{"今天状态不好","路途也许坎坷","好家伙，直接死机","没有调味料"},
{"死机了","被制裁","你没有财运","没及格"},
{"被人嘲笑","被喷","心态崩溃","只有一包调味料"},
{"被人当成买面膜的","大凶","五行代码198个报错","路途坎坷"},
{"你失踪了","被人喷","阅读量1","被人嘲笑"},
{"报错19999+","电脑卡死，发现刚才做的demo全没了","被人看穿","被人陷害"},
{"被人陷害","WA","被队友炸死","别人发现了Bug"},
{"没有财运","99+报错","连不上了","503个Bug"},
};
int w_sum = 0;
int length = sizeof(w_list) / sizeof(int);
for (int i = 0; i < length; i++){
w_sum += w_list[i];
}
int randVal = rand() % w_sum;
int rward = 0;
for (int i = 0; i < length; i++){
if (randVal <= w_list[i]){
rward = i;
break;
}
randVal -= w_list[i];
}
cout<<"                你的运势是:"<``````
``` 拜拜！ 再见！ 这次的博客就写到这里，因为作者关注量已经到了2000，所以关注我的可能有一些不能会关了，请谅解。 拜拜   ```
``` ```
``` ```
``` ```
``` ```
``` ```
``` ```
``` ```
``` ```
``` 你可能感兴趣的 Java开发中，spring mvc 的线程怎么调用？ 小麦麦子 springmvc maven依赖范围 bitcarter maven Jaxb org.xml.sax.saxparseexception : premature end of file darrenzhu xmlprematureJAXB CSS Specificity 周凡杨 html权重Specificitycss java与servlet g21121 servlet eclipse中安装maven插件 510888780 eclipsemaven jpa@OneToOne关联关系 布衣凌宇 jpa 我的spring学习笔记11-Spring中关于声明式事务的配置 aijuans spring事务配置 按字母分类： ABCDEFGHIJKLMNOPQRSTUVWXYZ其他 首页 - 关于我们 - 站内搜索 - Sitemap - 侵权投诉 版权所有 IT知识库 CopyRight © 2000-2050 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号 ```