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.

How to Generate a Sequence of Unique Random Integers (2012)

4 pointsby vince14over 4 years ago

1 comment

ChrisLomontover 4 years ago
Use any period 2^32 PRNG, which can easily be done with 4 bytes of storage. He used 8.<p>A simple LCG is of the form<p>seed &lt;- A*seed + B<p>Start with any seed, in 32 bit unsigned int, assume 32 bit wrapping like in C&#x2F;C++, etc. (or do mod yourself), pick good A and B, and away you go.<p>The Hull–Dobell Theorem tells you how to pick A and B for this size m=2^32: A and B are both odd and larger than 1 and A-1 is divisible by 4.<p>Finding nice A and B is a bit tricky - in which case look online for good choices or related theory.<p>Of course, a LCG may not be as random as you like. In that case one of the PCG variants solve the problem elegantly.