From 4ac98d9791e7343816082f4b6eceac04e358db05 Mon Sep 17 00:00:00 2001 From: wsy182 <2392948297@qq.com> Date: Fri, 8 Aug 2025 09:47:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simulate_monty_hall.py | 66 ++++++++++++++++++++++++++++++++++++++++++ testGeneratefile.py | 33 +++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 simulate_monty_hall.py create mode 100644 testGeneratefile.py diff --git a/simulate_monty_hall.py b/simulate_monty_hall.py new file mode 100644 index 0000000..1fe5738 --- /dev/null +++ b/simulate_monty_hall.py @@ -0,0 +1,66 @@ +import random + + +def simulate_monty_hall(switch, num_trials=100000): + """ + 模拟三门悖论(蒙提霍尔问题) + + 参数: + - switch: bool, 是否在主持人打开门后选择换门 + - num_trials: int, 模拟次数(默认10万次) + + 返回: + - 获胜次数 + - 胜率 + """ + win_count = 0 + doors = [1, 2, 3] # 三扇门 + + for _ in range(num_trials): + # 随机放置汽车(1 表示汽车,0 表示山羊) + car_door = random.choice(doors) + + # 参赛者初始选择 + player_choice = random.choice(doors) + + # 主持人要打开一扇门:不能是参赛者选的,也不能是有车的 + remaining_doors = [d for d in doors if d != player_choice and d != car_door] + host_opens = random.choice(remaining_doors) + + # 换门逻辑 + if switch: + # 换到剩下的那一扇未被选、未被开的门 + final_choice = [d for d in doors if d != player_choice and d != host_opens][0] + else: + # 不换,保持原选择 + final_choice = player_choice + + # 判断是否获胜 + if final_choice == car_door: + win_count += 1 + + win_rate = win_count / num_trials + return win_count, win_rate + + +# === 运行模拟 === +if __name__ == "__main__": + num_trials = 100000 + + print(f"模拟 {num_trials} 次三门问题:\n") + + # 情况1:坚持不换门 + wins_stay, rate_stay = simulate_monty_hall(switch=False, num_trials=num_trials) + print(f"坚持原选择(不换门):") + print(f" 获胜次数: {wins_stay}") + print(f" 胜率: {rate_stay:.4f} ({rate_stay * 100:.2f}%)\n") + + # 情况2:总是换门 + wins_switch, rate_switch = simulate_monty_hall(switch=True, num_trials=num_trials) + print(f"总是换门:") + print(f" 获胜次数: {wins_switch}") + print(f" 胜率: {rate_switch:.4f} ({rate_switch * 100:.2f}%)\n") + + print("理论值对比:") + print(" 不换门胜率: 1/3 ≈ 33.33%") + print(" 换门胜率: 2/3 ≈ 66.67%") \ No newline at end of file diff --git a/testGeneratefile.py b/testGeneratefile.py new file mode 100644 index 0000000..5d48e60 --- /dev/null +++ b/testGeneratefile.py @@ -0,0 +1,33 @@ +import os +import random + +# 输出目录 +output_dir = "./output_files" +os.makedirs(output_dir, exist_ok=True) + +# 配置 +file_count = 40 +min_size_mb = 70 +max_size_mb = 80 +chunk_size = 1024 * 1024 # 每次写入 1MB + + +def generate_random_file(file_path, size_bytes): + with open(file_path, "wb") as f: + written = 0 + while written < size_bytes: + chunk = os.urandom(min(chunk_size, size_bytes - written)) + f.write(chunk) + written += len(chunk) + + +for i in range(1, file_count + 1): + file_size_mb = random.randint(min_size_mb, max_size_mb) + file_size_bytes = file_size_mb * 1024 * 1024 + filename = f"file_{i}.bin" + filepath = os.path.join(output_dir, filename) + + print(f"Creating {filename} ({file_size_mb} MB)...") + generate_random_file(filepath, file_size_bytes) + +print("✅ 所有文件生成完毕。") \ No newline at end of file