# Is RandomInteger significantly different from RandomReal or RandomVariate[UniformDistribution]?

Consider the following thought experiment:

Take a sample of n points and calculate the Mean and StandardDeviation of the points in the sample. Next, take another sample of the same size as before ie with n points and repeat the calculations, as before.

At this point, what you have are two points, 2x1 in dimension, where the x coordinate is a mean and the y coordinate is a standard deviation.

Continue taking samples of points of size n and calculating their statistics as described above. When you reach a point where you have m pairs of calculated statistics, stop and consider the next step.

Now, take the mean and standard deviation of the collection of m 2x1 points you have amassed. In order to do that, you need to treat each dimension in the data separately. Starting with the means (coordinate x), the mean you calculate, is the mean value of the means of the m different samples of n points each, from earlier. The standard deviation of the the x's, is, also, the standard deviation of the means.

Working in the same fashion for the y coordinates (the standard deviations) produces, like before, a 2x1 pair of mean-standard deviation point of the standard deviations.

At this point you have repeatedly sampled a process m times, and have taken n measurements each time. The mean and standard deviation of this process have a (joint) distribution themselves. That distribution is summarized by the four numbers you have calculated at the end of the previous step.

Having gotten so far, consider what will happen to the numbers you have calculated, if you are to switch from the process you were observing before, to a new-somehow-similar process. You would expect to find that the sampling distribution you identified before will be similar to the one from the new process, because of the similarity between the two processes.

And when you don't observe that, you are forced to revise your assumptions about the similarity of the processes, right?

OK, now stop thinking and consider how you can get random integers in Mathematica.

I know of two ways, namely use RandomInteger[range,dims] or Round[] the output of RandomReal[range,dims] or RandomVariate[UniformDistribution[range],dims]. Using the later method, produces a graph much like this next one:

Both functions seem to produce random samples that exhibit the same sampling distributions for the mean and the standard deviation.

What will come next is a bit puzzling to me and is what motivated this post.

Consider now taking repeatedly samples from RandomInteger. The next graph uses sampling from the Round[RadomReal] approach, for the sake of comparison with the previous graph.

The pattern you observe is consistently displayed across samples. In effect, Round[RandomReal] produces integers with lower (mean) variability as does the RandomInteger function!

(The range I have used for creating these graphs is (6,13) and I have taken 1000 samples, each 100 points wide).

I have used statistical testing to verify that the difference is not a visual artifact. The results of testing are presented in the following table:

The first column of the table reports the results of using a t-Student test for mean equality across samples for the means (x-coordinate) and the second column reports the same results for the means of the y-coordinate.

The formal testing verifies that, indeed, the sampling distribution of random integers produced by RandomInteger produces higher variability (higher mean variance)!

(I have tested for variance equality too; the results are what is expected from looking at the graphs; similar tests are run for the case of the samples used to construct Graph 1 above- the results verified sampling distribution similarity in that case)

Does anybody have a clue why this is the case?

update: the code I used is here

update2: after a suggestion from J.M. I incorporated a change into the code for reproducibility reasons. Now, the random generators in testdatasets[] should be enclosed in a BlockRandom[]. The first call in the block is a call to SeedRandom[123456789987].

update3: I have updated the graphs in the body of the text to match the ones you'd get if you used the following parameters for testing my code

range = {6, 13};
dims = {100, 1000};
repeat = 10;
results = testresults[range, dims, repeat];


and then inspected results[[1]].

update4: I have updated the code to take into account the suggestion made by J.M. of taking into consideration the DiscreteUniformDistribution[]. It is true that this function produces samples that are consistent with RandomInteger[].

• Include the code you used then this post may get more attention. – Edmund Aug 20 '17 at 12:35
• Yes, it is. Take The Tour and look at the many other questions posted here. – Edmund Aug 20 '17 at 12:37
• If it is quite long, you can put the code on Pastebin. – J. M. will be back soon Aug 20 '17 at 13:01
• One more possible extension: you might wish to look at RandomVariate[DiscreteUniformDistribution]. – J. M. will be back soon Aug 20 '17 at 14:55
• I think the explanation is simple. RandomInteger[{6, 13}] produces each of the integers 6 through 13 with equal probability, while Round @ RandomReal[{6, 13}] produces the integers from 7 through 12 with equal probability, but 6 and 13 are only produced half as often. Basically, Round @ RandomReal[{6, 13}] is equivalent to RandomChoice[{1, 2, 2, 2, 2, 2, 2, 1} -> {6, 7, 8, 9, 10, 11, 12, 13}]. Since 6 and 13 occur less often, the standard deviation will be smaller. – Carl Woll Aug 20 '17 at 15:06