【建站服务】广州爱采购会员/58同城会员账户/百度竞价开户/百家号注册运营/推广公司网站-网站营销全网推广-域名申请
作者: 风兰 . 阅读量: 6 . 发表时间:2022-09-21 02:17:53
上往建站提供服务器空间服务商,百度快照排名,网站托管,百度推广运营,致力于设计外包服务与源代码定制开发,360推广,搜狗推广,增加网站的能见度及访问量提升网络营销的效果,主营:网站公司,百度推广公司电话,官网搭建服务,网站服务企业排名,服务器空间,英文域名等业务,专业团队服务,效果好。
广州爱采购会员/58同城会员账户/百度竞价开户/百家号注册运营/推广公司网站-网站营销全网推广

for (i = 0; i < number; i++)
{
// 生成实际的随机数
a[i] = rand() % 100;
if (a[i] > max){
max = a[i];
best = i;//获取手气最佳
}
suma += a[i];
}
for (i = 0; i < number - 1; i++)
{
b[i] = a[i] / suma * total;//按照随机数计算每个人实际获得的金额
sumb += round(b[i] * 100) / 100.0;//将红包金额保留两位小数
//输出信息
cout << "第" << setiosflags(ios::right)<< setw(3) << i + 1 <<
"个人的红包是:" << setiosflags(ios::right) << setw(6) <<
setiosflags(ios::fixed) << setprecision(2) <<
round(b[i] * 100) / 100.0 ;
if (best == i){
cout << "(手气最佳)" << endl;
}
else {
cout << endl;
}
}
//最后一人的红包金额等于总金额减去前面的金额。
cout << "第" << setiosflags(ios::right)<<
setw(3) << number << "个人的红包是:" <<
setiosflags(ios::right) << setw(6) << setiosflags(ios::fixed) <<
setprecision(2) << round((total - sumb) * 100) / 100.0;
if (best == number - 1){
cout << "(手气最佳)" << endl;
}
else {
cout << endl;
}
return 0;
}
结果:
请输入红包金额:100
请输入抢红包人数:10
第 1个人的红包是: 0.38
第 2个人的红包是: 3.06
第 3个人的红包是: 11.66
第 4个人的红包是: 4.40
第 5个人的红包是: 17.02
第 6个人的红包是: 14.53
第 7个人的红包是: 6.12
第 8个人的红包是: 13.19
第 9个人的红包是: 11.85
第 10个人的红包是: 17.79(手气最佳)
请按任意键继续. . .
adults
adults
223***32@qq.com
4年前 (2018-11-01)
是橡皮啊
289***4759@qq.com
25
window下的RAND_MAX为:0x7fff=2^15-1=32767
linxu下的RAND_MAX为:2^31-1=2147483647
不妨,就Windows下进行说明:
Rand函数返回返回值是0到RAND_MAX(32767) 范围内的一个(伪)随机整数。
取指定区间的(伪)随机数不建议采用“模除+加法”的方式,
譬如:如果采用此法去0-10000内的随机数,则写法为
srand( (unsigned)time( NULL ) );
int n = rand()%10000;
则0-2767之间每个数出现的概率为4/32676,而2768-9999之间的书出现的概率为3/32676,和前者是不同的。不过rand()产生的是伪随机数了这个无关紧要,哈哈哈 。
建议采用如下方式:
int u = (double)rand() / (RAND_MAX + 1) * (range_max - range_min)+ range_min
一下是VS开发文档示例:
// crt_rand.c
// This program seeds the random-number generator
// with the time, then exercises the rand function.
//
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void SimpleRandDemo( int n )
{
// Print n random numbers.
int i;
for( i = 0; i < n; i++ )
printf( " %6d ", rand() );
}
void RangedRandDemo( int range_min, int range_max, int n )
{
// Generate random numbers in the half-closed interval
// [range_min, range_max). In other words,
// range_min <= random number < range_max
int i;
for ( i = 0; i < n; i++ )
{
int u = (double)rand() / (RAND_MAX + 1) * (range_max - range_min)
+ range_min;
printf( " %6d ", u);
}
}
int main( void )
{
// Seed the random-number generator with the current time so that
// the numbers will be different every time we run.
srand( (unsigned)time( NULL ) );
SimpleRandDemo( 10 );
printf(" ");
RangedRandDemo( -100, 100, 10 );
}
/***********************************分割线*****************************************/
建议使用random库生成真随机数,如下:
#include <random>
#include <iostream>
using namespace std;
int main()
{
random_device rd; // non-deterministic generator
mt19937 gen(rd()); // to seed mersenne twister.
uniform_int_distribution<> dist(1,6); // distribute results between 1 and 6 inclusive.
for (int i = 0; i < 5; ++i) {
cout << dist(gen) << " "; // pass the generator to the distribution.
}
cout << endl;
}
输出如下:
5 1 6 1 2
参考链接:
【1】https://docs.microsoft.com/zh-cn/cpp/standard-library/random?view=vs-2019
【2】https://docs.microsoft.com/zh-cn/cpp/c-runtime-library/reference/rand?view=vs-201
广州爱采购会员/58同城会员账户/百度竞价开户/百家号注册运营/推广公司网站-网站营销全网推广
上往建站提供搭建网站,域名注册,官网备案服务,网店详情页设计,企业网店,专业网络店铺管理运营全托管公司咨询电话,服务器空间,微信公众号托管,网页美工排版,致力于域名申请,竞价托管,软文推广,全网营销,提供标准级专业技术保障,了却后顾之忧,主营:虚拟主机,网站推广,百度竞价托管,网站建设,上网建站推广服务,网络公司有哪些等业务,专业团队服务,效果好。
服务热线:400-111-6878 手机微信同号:18118153152(各城市商务人员可上门服务)
关键词:网站建设,企业网站,网站制作,网页设计,高端网站建设,企业网站制作,网页制作,制作网站,网站设计,高端网页设计,高端网站设计,做网站,自适应网站


