QQ空间代码大全:
新浪博客代码大全:
网易博客代码大全:
51博客代码大全:
搜狐博客代码大全:
博客状态栏代码大全:
博客图片代码大全:
博客文字代码大全:
博客导航链接大全:
博客鼠标代码大全:
博客分割线大全:
博客写字板素材大全:
新浪博客背景代码大全:
新浪博客素材鼠标指针大全:
博客flash以及地址大全:
博客flash挂件素材大全:
QQ空间代码│非主流图片:
新浪博客代码:
网易博客代码:
51博客代码:
163博客代码:
搜狐博客代码:
新浪博客背景代码:
博客播放器代码:
新浪博客模板代码:
blog代码:
博客时钟代码:
新浪博客模版代码:
新浪博客鼠标代码:
sina博客代码:
博客背景代码:
博客背景音乐代码:
博客更改首页留言处的文字颜色代码:
博客更改文章列表处的文字颜色代码:
博客更换留言处的图片代码:
博客免费网络书签登陆入口:
博客精巧实用IP地址查询代码:
迎接2008奥运会到计时免费代码:
博客免费论坛登陆口代码:
博客在线天气预报免费代码:
博客六种天气预报代码:
博客算卦的小东西:
关于在您主页怎样加入别人主页的链接:
博客滚动的文本框代码:
博客QQ及时通话:
状态栏文字从右往左移动:
博客电脑算命代码:
纯真网络显示IP地址代码:
上下滚动图片的代码:
博客滚动公告的代码:
左右飘动的图片代码:
博客不停滚动文字的代码:
博客文字特殊效果代码:
图片上飘动的文字代码:
在图片上写文章的代码:
博客竖排显示文字代码:
关于在图片旁边写文章的代码:
两种状态栏上的文字代码:
博客天气预报代码:
在博客加入你自己的视频:
涂鸦留言板的代码:
国外的一款涂鸦板代码:
博客定位代码:
博客停留时间代码:
右上角翻页效果代码:
首页模板全部透明的代码:
QQ12星座Flash代码:
个性公告栏代码:
图文友情链接的美丽效果:
两款幻灯片的代码:
自定义导航栏源代码:
滚动文字的友情链接代码:
一款漂亮的链接栏代码:
博客自动跳转到另外一个页面:
博客荧光棒鼠标指针:
博客加高首页横幅的代码:
几款QQ空间开场flash地址及代码:
博客鼠标指针后飘动的文字:
模糊文字的:
博客链接变色的代码:
博客触到链接是出现虚线的代码:
博客从底部弹出的小窗口代码:
博客阅读式公告栏代码:
博客探照灯效果:
c n=意思是算命。
84行C 代码教你实现洛谷占卜功能
因为我们要随机用户的运势,但是不可能每种运势的几率都相等,所以需要生成带权重的随机数
看到这个需求,先百度一下
百度到了这个代码
#include<iostream>
#include<vector>
#include<numeric>
#include<ctime>
#include<cstdlib>
using std::vector;
using std::rand;
using std::srand;
using std::cout;
using std::endl;
class MyMath{
public:
vector<int> GetRandomNumWithWeight(vector<int> weight,int number){
int size= weight.size();
vector<int> 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~ weight0为1,weight0 1~ weight1为2,依次类推
for(int j= 0;j< size;j )
{
tempSum = weightj;
cout<< randomNnm<< endl;
if(randomNnm<= tempSum)
{
res.push_back(j 1);
break;
}
}
}
return res;
}
};
int main()
{
vector<int> weight={1000, 2000, 3000, 1000, 1000, 500, 500, 500, 500};//数字1-9的权重(这里的数字范围与权重都可以自定义)
MyMath myMath;
vector<int> result= myMath.GetRandomNumWithWeight(weight,5);
for(auto const&num:result)
{
cout<< num<<'';
}
cout<< endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
这个代码可以实现我们想要的随机数效果,
原理很简单,随机数ranIndex生成的区间为权重的总和,根据权重分割子区间。
但代码有点复杂,其实没必要辣么麻烦
所以,还是自己动手,丰衣足食!!!
大概的原理如下:
我们先定义一个整数数组 w_list,用来存储我们随机的权重。
再定义一个w_sum,用来存权重总和。
再定义一个 lenth里面存数组的长度int length= sizeof(w_list)/ sizeof(int);
然后,一个for循环,用w_sum把w_list的每一项累加起来。
再int一个randVal,把每一份权重存到里面。int randVal= rand()% w_sum;
这一步可能有点难懂,举个例子,一共有100份权重(权重总和是100),我们用rand()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()0, and the result is each weight.