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.

Math.random() and 32-bit precision

32 pointsby jandemover 9 years ago

1 comment

kijinover 9 years ago
It&#x27;s nice to make full use of all the significant bits in a double-precision float, but I don&#x27;t think it matters much in typical use cases.<p>Mike Malone&#x27;s original code sample [1] uses only 6 bits from each invocation of Math.random(). Most people who use the multiply-and-floor method to obtain a random integer are probably using no more than 31 bits, either.<p>On the other hand, even the full 53 bits is woefully insufficient for unique identifiers or any serious cryptographical purposes, where you need at least 128 bits to be safe. So you&#x27;ll still need to call Math.random() multiple times.<p>[1] <a href="https:&#x2F;&#x2F;medium.com&#x2F;@betable&#x2F;tifu-by-using-math-random-f1c308c4fd9d" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;@betable&#x2F;tifu-by-using-math-random-f1c308...</a>