iPAS 考題解析以下 Python 程式碼執行後,輸出結果為何? ```python import random random.seed(42) results = [random.random() < 0.3 for _ in range(5)] count = results.count(True) print(count / len(results)) ```
你的作答
在題目頁作答不會存進學習紀錄,想留下紀錄可以到題庫練習。
解析
正確答案:D. 0.6
此題需要實際執行:random.seed(42) 後,random.random() 5次的結果取決於種子。使用種子 42 時,random.random() 的 5 個值中有 3 個小於 0.3(即 True),count=3,輸出 3/5=0.6。實際用 Python 執行:seed(42)後5次random.random()約為[0.639, 0.025, 0.275, 0.223, 0.736],其中 0.025、0.275、0.223 小於 0.3,count=3,3/5=0.6。
每個選項為什麼對或錯
-
A 0.2 錯誤選項
0.2 = 1/5,不符合種子 42 的計算結果。
-
B 0.3 錯誤選項
0.3 是期望值,不是特定種子的實際結果。
-
C 0.4 錯誤選項
0.4 = 2/5,不符合種子 42 的計算結果。
-
D 0.6 正解
0.6 = 3/5,種子 42 的實際計算結果。
答案、題目或解析有問題?回報這題