Comments on: Random Integers http://mattsnider.com/languages/javascript/random-integers/ Understanding JavaScript and Frameworks Sun, 07 Sep 2008 04:27:52 +0000 http://wordpress.org/?v=2.1.2 By: Michael http://mattsnider.com/languages/javascript/random-integers/#comment-15223 Michael Mon, 19 May 2008 15:22:49 +0000 http://mattsnider.com/languages/javascript/random-integers/#comment-15223 "The first problem is that random numbers on computers always have the inherit flaw of using the system clock to generate them. " Um... no. There are plenty of sources of pseudo-randomness on a computer (any hardware interrupts, not just the clock), and of course computers that are online can obtain truly random seeds. "A random integer generator should have an equal distribution between all desired integers." Also wrong. You're effectively taking Math.random() mod 10 to get the last digit. But the range of Math.random() is not evenly divisible by 10, so it cannot possibly return every decimal digit with equal frequency. There are better algorithms available than Math.random() -- look at the Mersenne Twister or GFSRs, both of which are faster and have better randomness than your Math.RandomInteger. “The first problem is that random numbers on computers always have the inherit flaw of using the system clock to generate them. ”

Um… no. There are plenty of sources of pseudo-randomness on a computer (any hardware interrupts, not just the clock), and of course computers that are online can obtain truly random seeds.

“A random integer generator should have an equal distribution between all desired integers.”

Also wrong. You’re effectively taking Math.random() mod 10 to get the last digit. But the range of Math.random() is not evenly divisible by 10, so it cannot possibly return every decimal digit with equal frequency.

There are better algorithms available than Math.random() — look at the Mersenne Twister or GFSRs, both of which are faster and have better randomness than your Math.RandomInteger.

]]>
By: Matt Snider http://mattsnider.com/languages/javascript/random-integers/#comment-15224 Matt Snider Mon, 19 May 2008 16:11:37 +0000 http://mattsnider.com/languages/javascript/random-integers/#comment-15224 To you first point, there was an issue I recall from my CSS schooling days, about the inherit flaw of all computer based randomizers. However, while there are many flaws (just do a google search), I didn't find the answer I was looking for, so I removed the offending line. Also, 'Math.random' is seeded with the current time. Your second point is completely wrong, at least as far as the documentation for 'Math.random' is written. 'Math.random' chooses a random number between 0 (inclusive) and 1. Maybe you know something about the source-code for JavaScript 'Math.random' that I do not, but true randomness, will have a fairly even distribution of numbers between 0 and 1 when run a large number of times. Therefore, if you convert these to integers properly, you will have a fairly even distribution of integers between your min and max. Although, since it is truly random, it is possible in a sample of 100 tries to randomly get 1 each time, it is highly unlikely, and repeated testing (see test page) shows that the distribution is fairly even. The point of this article, was to show how to use 'Math.random' to generate random integers. I made no statements that this was the best random number generator available. I am glad to hear that there are more efficient and effective random number generators and have included a link to MT at the bottom of my post. To you first point, there was an issue I recall from my CSS schooling days, about the inherit flaw of all computer based randomizers. However, while there are many flaws (just do a google search), I didn’t find the answer I was looking for, so I removed the offending line.

Also, ‘Math.random’ is seeded with the current time.

Your second point is completely wrong, at least as far as the documentation for ‘Math.random’ is written. ‘Math.random’ chooses a random number between 0 (inclusive) and 1. Maybe you know something about the source-code for JavaScript ‘Math.random’ that I do not, but true randomness, will have a fairly even distribution of numbers between 0 and 1 when run a large number of times. Therefore, if you convert these to integers properly, you will have a fairly even distribution of integers between your min and max. Although, since it is truly random, it is possible in a sample of 100 tries to randomly get 1 each time, it is highly unlikely, and repeated testing (see test page) shows that the distribution is fairly even.

The point of this article, was to show how to use ‘Math.random’ to generate random integers. I made no statements that this was the best random number generator available. I am glad to hear that there are more efficient and effective random number generators and have included a link to MT at the bottom of my post.

]]>
By: MillsJROSS http://mattsnider.com/languages/javascript/random-integers/#comment-15253 MillsJROSS Tue, 20 May 2008 12:26:07 +0000 http://mattsnider.com/languages/javascript/random-integers/#comment-15253 I didn't even consider zero every being selected. That's a nice piece information there. I like to attach a random function to my Number object, with the addition of a _ function, which takes a value and simply returns that value. So I can write _(3).random(); and get a random number 1 through 3. Zero returns zero, negative numbers go from -N to -1. I also have an object called Range that would work something like this Range("[4,7)").random(); In this case it would return 4, 5, or 6. It's not really an improvement on your functionality. For some reason, I just don't like using the Math object. Good post! I didn’t even consider zero every being selected. That’s a nice piece information there.

I like to attach a random function to my Number object, with the addition of a _ function, which takes a value and simply returns that value. So I can write

_(3).random();

and get a random number 1 through 3. Zero returns zero, negative numbers go from -N to -1. I also have an object called Range that would work something like this

Range(”[4,7)”).random();

In this case it would return 4, 5, or 6. It’s not really an improvement on your functionality. For some reason, I just don’t like using the Math object. Good post!

]]>
By: Matt Snider http://mattsnider.com/languages/javascript/random-integers/#comment-15281 Matt Snider Wed, 21 May 2008 15:54:05 +0000 http://mattsnider.com/languages/javascript/random-integers/#comment-15281 Thanks for the comment MillsJROSS. I like the idea of attaching random to Number. Also, it's good to note that the method discussed in this article, doesn't support 0 or negative numbers well. Thanks for the comment MillsJROSS. I like the idea of attaching random to Number. Also, it’s good to note that the method discussed in this article, doesn’t support 0 or negative numbers well.

]]>