#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 抽卡函数
int draw_cards(int ur_cards, int total_cards, int num_cards) {
int ur_count = 0;
for (int i = 0; i < num_cards; i++) {
if (rand() % total_cards < ur_cards) {
ur_count++;
}
}
return ur_count;
}
// 模拟一场十连
int simulate_game(int *ur_found_last_10) {
int total_cards = 1000;
int ur_cards = 25;
int ur_card_count_in_game = 0;
for (int round = 1; round <= 9; round++) {
int ur_cards_in_round = draw_cards(ur_cards, total_cards, 8);
ur_card_count_in_game += ur_cards_in_round;
}
// 第10包逻辑
if (*ur_found_last_10 == 0) {
// 上一次十连未抽到UR,第10包保底出1张UR
ur_card_count_in_game += draw_cards(ur_cards, total_cards, 7);
ur_card_count_in_game += 1;
} else {
// 上一次十连抽到UR,第10包按20%概率出UR
ur_card_count_in_game += draw_cards(ur_cards, total_cards, 7);
ur_card_count_in_game += draw_cards(1, 5, 1); // 20%概率
}
// 更新当前十连是否有UR
*ur_found_last_10 = (ur_card_count_in_game > 0);
return ur_card_count_in_game;
}
int main() {
srand(time(NULL));
int total_games = 1000000; // 设置模拟的总次数
long long total_ur_cards = 0;
int ur_found_last_10 = 1; // 初始化第0次十连状态为“抽到UR”
for (int i = 0; i < total_games; i++) {
total_ur_cards += simulate_game(&ur_found_last_10);
}
// 计算期望
double expected_ur_cards = (double)total_ur_cards / total_games;
printf("每次十连抽到UR的期望数量: %.4f\n", expected_ur_cards);
return 0;
}
#include <stdlib.h>
#include <time.h>
// 抽卡函数
int draw_cards(int ur_cards, int total_cards, int num_cards) {
int ur_count = 0;
for (int i = 0; i < num_cards; i++) {
if (rand() % total_cards < ur_cards) {
ur_count++;
}
}
return ur_count;
}
// 模拟一场十连
int simulate_game(int *ur_found_last_10) {
int total_cards = 1000;
int ur_cards = 25;
int ur_card_count_in_game = 0;
for (int round = 1; round <= 9; round++) {
int ur_cards_in_round = draw_cards(ur_cards, total_cards, 8);
ur_card_count_in_game += ur_cards_in_round;
}
// 第10包逻辑
if (*ur_found_last_10 == 0) {
// 上一次十连未抽到UR,第10包保底出1张UR
ur_card_count_in_game += draw_cards(ur_cards, total_cards, 7);
ur_card_count_in_game += 1;
} else {
// 上一次十连抽到UR,第10包按20%概率出UR
ur_card_count_in_game += draw_cards(ur_cards, total_cards, 7);
ur_card_count_in_game += draw_cards(1, 5, 1); // 20%概率
}
// 更新当前十连是否有UR
*ur_found_last_10 = (ur_card_count_in_game > 0);
return ur_card_count_in_game;
}
int main() {
srand(time(NULL));
int total_games = 1000000; // 设置模拟的总次数
long long total_ur_cards = 0;
int ur_found_last_10 = 1; // 初始化第0次十连状态为“抽到UR”
for (int i = 0; i < total_games; i++) {
total_ur_cards += simulate_game(&ur_found_last_10);
}
// 计算期望
double expected_ur_cards = (double)total_ur_cards / total_games;
printf("每次十连抽到UR的期望数量: %.4f\n", expected_ur_cards);
return 0;
}