Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why are random numbers are severly skewed

Status
Not open for further replies.

bpeirson

Technical User
Sep 28, 2005
85
CA
I have run this code numerous times with different upper & lower bounds. I have deleted the randomize statement, included a single randomize statement. I have simplified the X1 = ... equations.

Code:
Sub Randoms()
Dim x As Integer, x1 As Integer, x2 As Integer, x3 As Integer, x4 As Integer, x5 As Integer
For x = 1 To 500

'Random # = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
Randomize
x1 = Int((Cells(200, 10) - Cells(200, 11) + 1) * Rnd + Cells(200, 11))
Randomize
x2 = Int((Cells(200, 10) - Cells(200, 11) + 1) * Rnd + Cells(200, 11))
Randomize
x3 = Int((Cells(200, 10) - Cells(200, 11) + 1) * Rnd + Cells(200, 11))
Randomize
x4 = Int((Cells(200, 10) - Cells(200, 11) + 1) * Rnd + Cells(200, 11))
Randomize
x5 = Int((Cells(200, 10) - Cells(200, 11) + 1) * Rnd + Cells(200, 11))
Cells(x, 1) = x1
Cells(x, 2) = x2
Cells(x, 3) = x3
Cells(x, 4) = x4
Cells(x, 5) = x5
Next

End Sub

The end result has always been the same. The upperbound number appears more often than the lowerbound number by a factor of 20:1. The numbers in between are skewed as well. A graph of frequency of each number is triangular, 0 at the low end and max at the left end.

I have always accepted that random numbers created with the RND() or RAND() functions are not truly random but I would expect a better distribution.

Is there an error in my code or is there a more reliable way to create a random number?
 
SORRY, ignore this post. Code is fine. User is at fault.

I used SUMIF rather than COUNTIF to tally the results.

Please dont hold this against me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top