0
$\begingroup$

I have a random number generator that generate integers within [0, r).

I want to write a piece of code to test whether the numbers from it are truly uniformly distributed using chi-square test.

I select r to be very big like 1,000,000, and I generate random integers 10,000,000 times. Then I get the X^2.

The next thing I need to do is to check the chi-square value against a degree of freedom table. But I am not allowed to have one, nor using some online distribution calculator.

What should I do?

Someone told me that I can just calculate an interval: r^2 - 2*r*(sqrt r) and r^2+2*r*(sqrt r), then to see whether the value fall in that interval or not. But he doesn't know the reason either.

Is the way above correct?

$\endgroup$

1 Answer 1

3
$\begingroup$

With the numbers you are talking about, you don't need a chi-squared table. You can use a normal approximation. If $X$ is chi-squared with $r$ degrees of freedom, then

$$ \frac{x-(r-1)}{\sqrt{2(r-1)}} \sim N(0,1) $$

Another approach would be to do a quantile plot of your observations against the theoretical quantiles of the uniform.

However, I'm not sure these tests are really going to answer your question about the merits of your random number generator. It's not sufficient that your numbers be uniformly distributed - you also want to show that they are independent and that are no periodicities in your set.

$\endgroup$
6
  • $\begingroup$ +1. For consistency with the problem statement, you might consider replacing $r$ by $r-1$, which is the df for the proposed test. $\endgroup$
    – whuber
    Jun 29, 2014 at 17:43
  • $\begingroup$ @whuber Are you sure about that? He isn't estimating any parameters from his simulated data .. he knows the theoretical mean and variance, and he should have r groups for the data. Or perhaps I'm missing something. $\endgroup$
    – Placidia
    Jun 29, 2014 at 17:48
  • 1
    $\begingroup$ You always lose one df from the constraint that the sum of the expected values must equal $r$. $\endgroup$
    – whuber
    Jun 29, 2014 at 17:59
  • $\begingroup$ what if the numbers are not uniformly distributed? i.e., for the number range [0, r) we have associated a probability for each integer and all probabilities sum to 1. In this case, can I still use normal approximation? Also, could you please explain more about the normal approximation? or some quick reference I can read? I am a developer and I threw back almost all my stats knowledge back to university $\endgroup$
    – Jack
    Jun 29, 2014 at 18:52
  • 1
    $\begingroup$ @Jack. The normal approximation applies to the chi-squared as the number of degrees of freedom gets large, whatever the origin of the chi-squared statistic. If your theoretical probabilities are correct - so the data follow that model - then $\sum \frac{(O-E)^2}{E}$ will be approximately chi-squared, where O and E are observed and expected counts respectively. So yes; if the model fits, the test statistic is chi-squared, which can be approximated by a normal. $\endgroup$
    – Placidia
    Jun 29, 2014 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.