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.

Predicting the next Math.random() in Java

150 pointsby nilknarfover 10 years ago

9 comments

imaginenoreover 10 years ago
If you want cryptographic-quality random numbers, both Java and Javascript have them. Math.random() is simply a super-fast decent RNG.<p>Example:<p><pre><code> var buf = new Uint32Array(10); window.crypto.getRandomValues(buf); console.log(buf); </code></pre> Outputs things like:<p><pre><code> [4027145128, 258543382, 1205615760, 2665675208, 4033127244, 2280027866, 3983484449, 510932333, 1911490534, 2609399642] </code></pre> This works in Chrome and FF.<p>IE11 has Crypto.getRandomValues(...)<p>Java has SecureRandom:<p><a href="http://docs.oracle.com/javase/6/docs/api/java/security/SecureRandom.html" rel="nofollow">http:&#x2F;&#x2F;docs.oracle.com&#x2F;javase&#x2F;6&#x2F;docs&#x2F;api&#x2F;java&#x2F;security&#x2F;Secur...</a>
评论 #8254296 未加载
评论 #8254092 未加载
mnw21camover 10 years ago
That is a nice not-so-subtle reminder. When a PRNG says it is insecure, <i>it is insecure</i>. When a PRNG says it is secure, it <i>might</i> be - get someone very clever to check it first.
phpnodeover 10 years ago
nitpick, Firefox doesn&#x27;t use Rhino, it uses SpiderMonkey which is C++.<p><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Mozilla&#x2F;Projects&#x2F;Sp...</a>
drinchevover 10 years ago
How dangerous this prediction can be? I can&#x27;t stop thinking of java-backended real money, poorly written, gaming websites.
评论 #8254172 未加载
评论 #8253951 未加载
评论 #8254162 未加载
评论 #8253952 未加载
评论 #8253985 未加载
xxsover 10 years ago
Math.random() should be used only for tests. That&#x27;s it. Performance sucks as it&#x27;s shared. ThreadLocalRandom is a lot better if you need fast but not-quality random.<p>And there is SecureRandom for security concerns.<p>Last fun fact Math.random() and a Monte Carlo test introduced &quot;CAS in Java&quot; and all that followed with JSR 166.
mdaover 10 years ago
Reminded me an interesting Java Random issue with small seeds and power of two intervals:<p><pre><code> for(int i = 0; i &lt; 256; i++) { System.out.println(new Random(i).nextInt(8)); } </code></pre> It returns same number for all seeds.
lunixbochsover 10 years ago
I tested a similar attack against ApacheCommons&#x27; RandomStringUtil. Given a few bytes of output, I could recover the RNG state in 20 minutes on CPU.
jlebarover 10 years ago
As another commenter has said, Firefox doesn&#x27;t use Rhino. Here&#x27;s the relevant code in Firefox&#x27;s JS engine.<p><a href="http://dxr.mozilla.org/mozilla-central/source/js/src/jsmath.cpp#765" rel="nofollow">http:&#x2F;&#x2F;dxr.mozilla.org&#x2F;mozilla-central&#x2F;source&#x2F;js&#x2F;src&#x2F;jsmath....</a>
Peksaover 10 years ago
Hah, funny! I recently did the same to circumvent CSRF-protection based on java.util.Random. Here&#x27;s my solver in JS: <a href="https://peks.as/experiments/random/" rel="nofollow">https:&#x2F;&#x2F;peks.as&#x2F;experiments&#x2F;random&#x2F;</a>