Random Number Gen Not Quite Random

Hi all. This is my first time posting here. I have a tiny problem. I am just playing around with VB 2010 Express and when I create a random number not all numbers come up equally. For example, when I create a random number from 0 to 10 the numbers 1 to 9 come up with equal frequency but the 0 and the 10 only get generated perhaps as few as half as often. It doesn't matter if I change it to randomize 0 to 3 either. In this case it generates the 1 and 2 with equal frequency but the 0 and 3, being the "fence posts", only come up half as often.

My Code:

Dim shtRandomNumber as short
Randomize()
shtRandomNumber = rnd()*10

Currently I have this in a button click event but once I noticed the problem I put it in a loop to run it 1000 times and kept some stats in an array and viola. I noticed that whatever the range I use, the lowest and highest possible values only get generated approximately half as frequent as all the other numbers in the set. How perplexing!



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories