# The limits of Rnd

As I promised earlier, I’ve run a histogram of the random class random.number method. I ran it for the smallest interval size of 1e-6, so the following histogram should be compared to the last two histograms in my earlier post.

As you can see it is markedly better than the Rnd( ) function, though it shows a bit more spikiness than the W-H algorithm.

I decided to reduce the interval by a further factor of ten so that each interval is 1e-7. First the Xojo random.number method:

Following is the W-H algorithm with the same interval size.

Again the Xojo random.number is spikier than W-H, but at this point I think we’ve reached the limits of what a histogram will tell us, and the random functions would need to be subjected to a battery of statistical tests to determine anything meaningful.

I found the original paper on-line and it said that ix, iy and iz should be seeded with random values in the range of 1-30000, but can’t find the more recent one.

For the newer 4 cycle algorithm should those values (and iw) be in the the same range or is the allowed range larger? If so what is it?

Thanks,
Karen

[quote=281990:@Robert Weaver]Now look at the Xojo Rnd( ) function with the same interval size:
The spikes occur at regular intervals,[/quote]

Can you show the code or spot what’s wrong with mine because I can’t reproduce that pattern, even when decreasing the interval size by 5. These plots are of the original 200000 intervals.

Wichmann-Hill

Rnd

[code] dim i, q, idx, bins(199999) As integer, v As double

for q = 0 to 9
for i = 0 to 1e8

``````  v = Rnd  //builtin Rnd
//v = Rnd3 //3 cycle Wichmann-Hill

idx = Floor(v * 200000)

bins(idx) = bins(idx) + 1

next
TextArea1.AppendText Str(q)
TextArea1.Refresh
``````

next[/code]

I use the Random class in an application that makes movies of points with random vector offsets. A Seed is specified for the first frame and each subsequent frame the Seed is incremented. This jitters the points in a reproducible way but a pattern emerges in the animation: each point follows a linear jumping sequence. (To get rid of the linearity I set the Seed to initialSeed+frame*frame instead of just initialSeed+frame)

Here’s 7 evenly spaced tiny red points with a fatter colored point at random offset. Nice and random.

Drawing 4 more frames on top you can see the lines starting

After 100 frames (and slightly dimming each old frame so lighter is older) you can see the regularity

This same behavior happens with the Wichmann-Hill algorithm and makes me think they are the same, or the same category of algorithm at least. Can’t test Rnd for this because there’s no seed.

Also, another source of random values is Xojo.Math.RandomInt.

Will, here is my code:

``````Sub TestRandom()
dim i,nSamples,q As int64
dim histArray(),nIntervals As Integer
dim r,intervalSize As  Double
dim rr As new Random
nSamples=1000000000
intervalSize=1.0e-6
nIntervals=300
ReDim histArray(nIntervals)
for i=1 to nSamples
// Comment out unused random functions
r=Rnd()  'Old Xojo Random function
'r=rr.Number  'Xojo Random class
'r=Rnd2()  'Wichmann-Hill (1982)
'r=Rnd3()  'Wichmann-Hill (2005)
q=min(nIntervals,floor(r/intervalSize))
histArray(q)=histArray(q)+1
next
histArray(nIntervals)=0 //delete the catch-all bucket
// Call method to plot vertical barchart from  array data
DrawHistogram(histArray)
End Sub``````

@Will S when I run your code and then just dump out the first 301 intervals similar to Robert’s approach in his initial charts, I do think I see the same periodic spikiness that Robert noted and would probably see this more clearly if the interval width was descreased as in his second example. DId your chart show just the first 300 points, or further aggregate bins ro show all 200k?

[code]dim i, q, idx, bins(199999) As integer, v As double

for q = 0 to 9
for i = 0 to 1e8

``````  v = Rnd  //builtin Rnd
//v = Rnd3 //3 cycle Wichmann-Hill

idx = Floor(v * 200000)

bins(idx) = bins(idx) + 1

next
'TextArea1.AppendText Str(q)
'TextArea1.Refresh
``````

next

for i =0 to 300
print str(bins(i)) 'then plot this trivially in excel
next[/code]