You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在CPython 3.x中验证random.SystemRandom().choice的离散值独立性与均匀分布

Answer

1. Cryptographic Security of random.SystemRandom().choice() in CPython 3.0-3.9

First off, you can fully rely on SystemRandom().choice() for cryptographic purposes across all these versions. Here's the breakdown:

  • SystemRandom doesn’t use Python’s regular pseudo-random number generator (PRNG). Instead, it directly taps into your operating system’s cryptographically secure random number generator (CSPRNG) — think /dev/urandom on Unix-like systems, CryptGenRandom on Windows, or equivalent OS-level sources.
  • These system CSPRNGs are built explicitly to produce values that are both independent (no predictable pattern between consecutive outputs) and uniformly distributed (each outcome has equal probability), which are exactly the properties required for cryptographic use cases.
  • The CPython 3.2 OpenSSL compatibility issue you noted is irrelevant to SystemRandom. That problem impacts other parts of the standard library (like the ssl module) but not this class, since it doesn’t depend on OpenSSL at all.

2. Verifying Uniform Distribution for Discrete Values (No Stats Background Needed)

Your histogram already looks great — all counts are extremely close to each other. To turn that visual intuition into concrete proof, the Chi-Squared Goodness-of-Fit Test is ideal for discrete uniform distributions, and it’s straightforward to run with scipy.

Step-by-Step with Code

Let’s use your histogram counts as an example. First, collect your observed counts into a list:

observed = [1675814, 1677567, 1677969, 1678564, 1676721, 1677222, 1679371, 1678663, 1676892, 1678433]

Then run the chi-squared test with scipy.stats.chisquare:

from scipy.stats import chisquare

# For a uniform distribution, expected counts = total observations / number of categories (10 here)
total = sum(observed)
expected = [total / 10] * 10

chi2_stat, p_value = chisquare(observed, expected)

print(f"Chi-Squared Statistic: {chi2_stat:.2f}")
print(f"P-Value: {p_value:.4f}")

How to Interpret the Result

  • The p-value is the key metric here. If it’s greater than 0.05, we can confidently say there’s no statistically significant difference between your observed counts and a perfect uniform distribution.
  • For your data: total observations are ~16,777,216, so the expected count per category is ~1,677,721. Your counts are all within ±2,000 of that value — the p-value will be far above 0.05, confirming uniformity.

Quick Sanity Check for Independence

Verifying strict cryptographic independence is complex without advanced stats, but a simple check is to look at consecutive selections:

  • Count how many times the same number is chosen back-to-back. For a uniform distribution with 10 options, the expected frequency is ~10% of your total trials.
  • If this percentage is close to 10%, it’s a good sign there’s no obvious correlation between consecutive choices.

3. Final Takeaway

  • SystemRandom().choice() is cryptographically safe across CPython 3.0-3.9 — its reliance on OS-level CSPRNGs guarantees the required independence and uniformity.
  • Your histogram is a strong visual indicator of uniformity, and the chi-squared test will confirm this quantitatively.

内容的提问来源于stack exchange,提问作者dstromberg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 21:22:49