Jump to content
Larry Ullman's Book Forums

Js Math.Random Lacking In Randomness

Recommended Posts

Not sure if anyone else has ever noticed this, but the JS Math.random method isn't very random in some browsers.

Specifically, in IE and Chrome browsers, it seems very random, but in Firefox and Opera, is doesn't seem very random at all. Randomness seems to be especially lacking in Opera from what I can tell in my limited testing.

I'm assuming that both Microsoft and Google have expanded the Math.random method in their browsers to make them more random, which would account for those browsers continuously being more random.


In order to make things more random, I am thinking about writing my own random method that would better seed the Math.random function, but I don't really know where to start.

I'm far from an expert on this topic, so if there are any math experts or computer scientists out there that can offer me some advice on how to better randomize the Math.random method (without getting too insane), I'd love to hear it.


Thank you.

Link to post
Share on other sites

How about


That would give you a random number between 1 and 100k i believe...

But if you are having a browser specific issue...

I too by no means am an amature js programmer let alone expert...

Link to post
Share on other sites

Well, the point is to be able to add randomness to something that isn't random. For example, if I simply multiple each "random" number I get from Math.random() by the same constant, the randomness between values will not change at all.


I should have mentioned this before, but I need to use Math.random() to randomize a series of numbers in an array. The following is the standard way to randomize a bunch of numbers in an array:


var arr = [0, 1, 2, 3,...,100, 101, 102];

arr.sort(function () {

 return 0.5 - Math.random();



Whenever Math.random isn't that random though, the resulting array ends up with a lot of consecutive values all clumped together.

I've read about better "seeding" the randomness by adding the milliseconds of the current time into the equations, etc., but even then, values can be clumped together.


Anyway, Jaepee, thank you for your suggestion. The search continues for adding more randomness.

Link to post
Share on other sites

Yes, I remember the first time trying to create a random number in C (or maybe it was C++) and it output 37. I ran it again, and it output 37. It was a random number, but consistently the same. It's not natural for computers to do things randomly, despite the fact that it seems to happen all the time.


I would take the seconds or milliseconds and multiply that times the randomly returned number, then format it back to being within the proper boundaries.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...