TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Making Python Less Random

25 pointsby azhenley10 months ago

3 comments

sgarland10 months ago
While I appreciate examining system calls, I feel like I must be missing something in TFA, because you can just seed your own instance of random.Random [0].<p>Trivial (and terrible) example – on every run past the first, the lists are identical:<p><pre><code> #!&#x2F;usr&#x2F;bin&#x2F;env python3 import pickle import random SEED = 42 if __name__ == &quot;__main__&quot;: r = random.Random(SEED) rand_l = None new_rand_l = [] try: with open(&quot;rand_list.dat&quot;, &quot;rb&quot;) as f: rand_l = pickle.load(f) except FileNotFoundError: pass for i in range(100): new_rand_l.append(r.randint(0, 1000)) if rand_l is not None: assert rand_l == new_rand_l else: with open(&quot;rand_list.dat&quot;, &quot;wb&quot;) as f: pickle.dump(new_rand_l, f) </code></pre> [0]: <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;random.html#random.Random.seed" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;random.html#random.Random....</a>
评论 #40928204 未加载
Numerlor10 months ago
Why not just patch the module in the entry point beforen others get a chance to import the preseeded methods?
ocular-rockular10 months ago
Just set seed ._.