[Webkit-unassigned] [Bug 22595] Strange Math.random() behavior (firs time almost always returns approx same number)

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Thu Dec 4 11:07:26 PST 2008


https://bugs.webkit.org/show_bug.cgi?id=22595





------- Comment #2 from dylanryan at mac.com  2008-12-04 11:07 PDT -------
(In reply to comment #1)
> (In reply to comment #0)
> > Math.random() seems to always return a very similar number the first time it is
> > called.
> 
> Yes, that's correct. Could you please explain why this is a problem? E.g.,
> would a separate API for a cryptographically strong random generator fix the
> problem for you?
> 

It doesn't necessarily need to be strong, it just would be nice if the first
random number was usable. ALl it would take is for the javascript engine to
call into Math.random once when the program is started and ignore the result,
that way the first result the user can possibly get is actually usable. 

For example, I first noticed this because I wanted to use JSC to handle complex
math for me in shell scripts because it is much much easier to write javascript
code to deal with numbers than bash code. I had reason to use random numbers,
and I had to throw the first result out every time because it was not random at
all.

Also, I'm sure plenty of web pages expect Math.random to be, well, essentially
random. If you set your home page to a page that used Math.random (a quick
inspection of sourcecode shows that Apple.com uses Math.random for something,
not sure what), you'd get the same result every time you re-launch the program
whereas the same thing in Firefox and you get a usable result every time.

Worse would be if you were embedding javascript core in another program and
depending on its results to be usably random. Given that the workaround is as
simple a matter of calling the function once and ignoring the result, it seems
that that could be easily done in the JavaScript Core itself (or wherever, I am
not familiar with the code myself) when it is initialized, so that to anyone
using it (on the web, in a shell, or embedded in a different application), the
first result they ask for is actually usable, rather than expecting every
program that wants random numbers to discover the problem on their own and
throw out the first number. Certainly, you'll never get every web page that
uses Math.random() to do that just in case they are loaded as the homepage in
Safari, there are just too many pages out there! 


-- 
Configure bugmail: https://bugs.webkit.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.



More information about the webkit-unassigned mailing list