工作半年后,也要学习别人写博客啦!今天准备尝试模拟微信的红包随机金额算法。
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
void EntryPoint(double money,int count){
double value = money;
srand((unsigned)time(NULL));
int max = money * 100;
for (int index = 1; index <count; index++)
{
value = rand()%max + 1;
value = value / 100;
for (; value > money - 0.1 * (count - index);)
{
value = rand() % max + 1;
value = value / 100;
}
money = money - value;
printf("this is the %d red packet,=%.2f\n",index,value,money);
}
printf("this is the %d red packet,=%.2f\n", count, money);
}
int main() {
double money = 4.88;
int count = 5;
EntryPoint(money,count);
return 0;
}
太久没有写程序了,好吃力啊,但是写成这样貌似不太科学,因为每次红包出来的金额都不是平均的,红包的金额之间差距很大。还需要改善!