website/content/blog/2017-03-07-uniformity-math-random.md

5.1 KiB
Raw Blame History

id title date author aliases permalink medium_post mf2_syndicate-to mf2_cite format tags
2095 Uniformity of Math.random() 2017-03-07T21:50:52+00:00 Brandon Rozek
/2017/03/uniformity-math-random/
/2017/03/uniformity-math-random/
O:11:"Medium_Post":11:{s:16:"author_image_url";N;s:10:"author_url";N;s:11:"byline_name";N;s:12:"byline_email";N;s:10:"cross_link";N;s:2:"id";N;s:21:"follower_notification";N;s:7:"license";N;s:14:"publication_id";N;s:6:"status";N;s:3:"url";N;}
a:1:{i:0;s:4:"none";}
a:4:{s:9:"published";s:25:"0000-01-01T00:00:00+00:00";s:7:"updated";s:25:"0000-01-01T00:00:00+00:00";s:8:"category";a:1:{i:0;s:0:"";}s:6:"author";a:0:{}}
aside
Statistics

There are many cases where websites use random number generators to influence some sort of page behavior. One test to ensure the quality of a random number generator is to see if after many cases, the numbers produced follow a uniform distribution.

Today, I will compare Internet Explorer 11, Chrome, and Firefox on a Windows 7 machine and report my results.

Hypothesis

H0: The random numbers outputted follow the uniform distribution

HA: The random numbers outputted do not follow the uniform distribution

Gathering Data

I wrote a small website and obtained my data by getting the CSV outputted when I use IE11, Firefox, and Chrome.

The website works by producing a random number using Math.random() between 1 and 1000 inclusive and calls the function 1,000,000 times. Storing its results in a file

This website produces a file with all the numbers separated by a comma. We want these commas to be replaced by newlines. To do so, we can run a simple command in the terminal

grep -oE '[0-9]+' Random.csv > Random_corrected.csv

Do this with all three files and make sure to keep track of which is which.

Here are a copy of my files for Firefox, Chrome, and IE11

Check Conditions

Since were interested in if the random values occur uniformly, we need to perform a Chi-Square test for Goodness of Fit. With every test comes some assumptions

Counted Data Condition: The data can be converted from quantatative to count data.

Independence Assumption: One random value does not affect another.

Expected Cell Frequency Condition: The expected counts are going to be 10000

Since all of the conditions are met, we can use the Chi-square test of Goodness of Fit

Descriptive Statistics

For the rest of the article, we will use R for analysis. Looking at the histograms for the three browsers below. The random numbers all appear to occur uniformly

rm(list=ls())
chrome = read.csv("~/Chrome_corrected.csv", header = F)
firefox = read.csv("~/Firefox_corrected.csv", header = F)
ie11 = read.csv("~/IE11_corrected.csv", header = F)
hist(ie11$V1, main = "Distribution of Random Values for IE11", xlab = "Random Value")

hist(firefox$V1, main = "Distribution of Random Values for Firefox", xlab = "Random Value")

hist(chrome$V1, main = "Distribution of Random Values for Chrome", xlab = "Random Value")

Chi-Square Test

Before we run our test, we need to convert the quantatative data to count data by using the plyr package

#Transform to count data
library(plyr)
chrome_count = count(chrome)
firefox_count = count(firefox)
ie11_count = count(ie11)

Run the tests

# Chi-Square Test for Goodness-of-Fit
chrome_test = chisq.test(chrome_count$freq)
firefox_test = chisq.test(firefox_count$freq)
ie11_test = chisq.test(ie11_count$freq)

# Test results
chrome_test

As you can see in the test results below, we fail to reject the null hypothesis at a 5% significance level because all of the p-values are above 0.05.

## 
##  Chi-squared test for given probabilities
## 
## data:  chrome_count$freq
## X-squared = 101.67, df = 99, p-value = 0.4069

firefox_test

## 
##  Chi-squared test for given probabilities
## 
## data:  firefox_count$freq
## X-squared = 105.15, df = 99, p-value = 0.3172

ie11_test

## 
##  Chi-squared test for given probabilities
## 
## data:  ie11_count$freq
## X-squared = 78.285, df = 99, p-value = 0.9384

Conclusion

At a 5% significance level, we fail to obtain enough evidence to suggest that the distribution of random number is not uniform. This is a good thing since it shows us that our random number generators give all numbers an equal chance of being represented. We can use Math.random() with ease of mind.