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.

There's Math.random(), and then there's Math.random() (2015)

35 pointsby corentin88over 1 year ago

5 comments

byronvickersover 1 year ago
Weird to see this show up on HN - I was just looking at this page a couple of days ago.<p>I have a little puzzle I&#x27;ve been trying to solve in my spare time; maybe someone here can point me in the right direction.<p>The puzzle is: given a float from Math.random(), suppose you know only whether it is greater than it is then 0.5 (i.e you only see the result of a coin flip which depends on Math.random()). What is a practical method to reverse out the state of the xorshift+ generator, given multiple such successive observations of the output?<p>Any input appreciated!
评论 #38194890 未加载
评论 #38193141 未加载
virexeneover 1 year ago
<i>“the 2⁵² numbers between 0 and 1 that double precision floating point can represent”</i><p>it&#x27;s a bit of a nitpick, but i believe there are 1023×2⁵² such numbers, which is quite a bit more. there are 2⁵² double precision floats in just [0.5;1)!
评论 #38194227 未加载
评论 #38194314 未加载
SeanLukeover 1 year ago
In Java there&#x27;s just Math.random(), and it&#x27;s been broken, with WONTFIX, since 1998.<p>Java&#x27;s random number generator returns exceptionally non-random values, but Sun, er, Oracle, won&#x27;t fix it because of the most insane of reasons: unlike in any reasonable language, Java&#x27;s PRNG essentially has a contract to be <i>deterministic</i>. There&#x27;s seemingly a worry that someone, somewhere, is actually relying on java.util.Random to always produce the same random number sequence for a given seed from Java version to version.
评论 #38195409 未加载
评论 #38195160 未加载
评论 #38195202 未加载
评论 #38195813 未加载
评论 #38199119 未加载
评论 #38197180 未加载
paulddraperover 1 year ago
I don&#x27;t understand why you&#x27;d want a floating point random.<p>It just seems like a terrible idea (as opposed to int)
评论 #38194721 未加载
评论 #38195538 未加载
评论 #38196225 未加载
评论 #38194445 未加载
评论 #38194964 未加载
paulpauperover 1 year ago
<i>Make no mistake however: even though xorshift128+ is a huge improvement over MWC1616, it still is not cryptographically secure. For use cases such as hashing, signature generation, and encryption&#x2F;decryption, ordinary PRNGs are unsuitable. The Web Cryptography API introduces window.crypto.getRandomValues, a method that returns cryptographically secure random values, at a performance cost.</i><p>If the 256 bit private key of the encryption is derived from a large character set ( A-Z, 1-9, etc.), it does not matter if the RNG is not perfect. I am assuming it&#x27;s not an online channel.<p>This is explained in more detail here <a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;cryptography&#x2F;comments&#x2F;fw2cdu&#x2F;can_you_retrieve_aes_key_that_was_generated_using&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;cryptography&#x2F;comments&#x2F;fw2cdu&#x2F;can_yo...</a>