Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
66
rated 0 times [  68] [ 2]  / answers: 1 / hits: 68024  / 15 Years ago, tue, june 30, 2009, 12:00:00

For 6 years I've had a random number generator page on my website. For a long time, it was the first or second result on Google for random number generator and has been used to decide dozens, if not hundreds of contests and drawings on discussion forums and blogs (I know because I see the referrers in my web logs and usually go take a look).



Today, someone emailed me to tell me it may not be as random as I thought. She tried generating very large random numbers (e.g., between 1 and 10000000000000000000) and found that they were almost always the same number of digits. Indeed, I wrapped the function in a loop so I could generate thousands of numbers and sure enough, for very large numbers, the variation was only about 2 orders of magnitude.



Why?



Here is the looping version, so you can try it out for yourself:



http://andrew.hedges.name/experiments/random/randomness.html



It includes both a straightforward implementation taken from the Mozilla Developer Network and some code from 1997 that I swiped off a web page that no longer exists (Paul Houle's Central Randomizer 1.3). View source to see how each method works.



I've read here and elsewhere about Mersenne Twister. What I'm interested in is why there wouldn't be greater variation in the results from JavaScript's built-in Math.random function. Thanks!


More From » random

 Answers
4

Given numbers between 1 and 100.




  • 9 have 1 digit (1-9)

  • 90 have 2 digits (10-99)

  • 1 has 3 digits (100)



Given numbers between 1 and 1000.




  • 9 have 1 digit

  • 90 have 2 digits

  • 900 have 3 digits

  • 1 has 4 digits



and so on.



So if you select some at random, then that vast majority of selected numbers will have the same number of digits, because the vast majority of possible values have the same number of digits.


[#99217] Thursday, June 25, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
wilson

Total Points: 27
Total Questions: 93
Total Answers: 93

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
wilson questions
Tue, Aug 9, 22, 00:00, 2 Years ago
Wed, May 11, 22, 00:00, 2 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
Wed, May 13, 20, 00:00, 4 Years ago
;