Smudge
2008-03-14 16:36:01 UTC
The Rnd() Function doesn't appear to always work as it should (according to
the documentation / MSDN info for the Rnd() Function):
{Begin Excerpt from VS 2008 documentation}
"
To produce random integers in a given range, use the following formula.
Visual Basic :
randomvalue = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))
Here, upperbound is the highest number in the range, and lowerbound is the
lowest number in the range.
"
{End Excerpt from VS 2008 documentation}
Sounds straight forward enough, however the following VB code demonstrates
that the Rnd() function does not always behave as it should:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim upperbound As Integer = 254
Dim lowerbound As Integer = 128
Dim randomvalue As Integer
Dim intX As Integer
Randomize()
For intX = 0 To 100000000
randomvalue = CInt(Int((upperbound - lowerbound + 1) * Rnd() +
lowerbound))
If randomvalue > upperbound Then Console.WriteLine("UPPER BOUND
EXCEEDED!!!: randomvalue=" & randomvalue & " But upperbound=" & upperbound)
Next
Console.WriteLine("Finished!")
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
The above code writes a a message to the console whenever the Rnd() function
returns a randomvalue that is greater than the upperbound specified (as per
the documentation).
On running this code I see that there are instances where thr Rnd function
does that.
I would be interested to see if other people's computers produce the same
results - it seems that they certainly should!
So is this a bug in Rnd() or is the VS/MSDN documentation wrong?
.
Many thanks.
the documentation / MSDN info for the Rnd() Function):
{Begin Excerpt from VS 2008 documentation}
"
To produce random integers in a given range, use the following formula.
Visual Basic :
randomvalue = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))
Here, upperbound is the highest number in the range, and lowerbound is the
lowest number in the range.
"
{End Excerpt from VS 2008 documentation}
Sounds straight forward enough, however the following VB code demonstrates
that the Rnd() function does not always behave as it should:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim upperbound As Integer = 254
Dim lowerbound As Integer = 128
Dim randomvalue As Integer
Dim intX As Integer
Randomize()
For intX = 0 To 100000000
randomvalue = CInt(Int((upperbound - lowerbound + 1) * Rnd() +
lowerbound))
If randomvalue > upperbound Then Console.WriteLine("UPPER BOUND
EXCEEDED!!!: randomvalue=" & randomvalue & " But upperbound=" & upperbound)
Next
Console.WriteLine("Finished!")
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
The above code writes a a message to the console whenever the Rnd() function
returns a randomvalue that is greater than the upperbound specified (as per
the documentation).
On running this code I see that there are instances where thr Rnd function
does that.
I would be interested to see if other people's computers produce the same
results - it seems that they certainly should!
So is this a bug in Rnd() or is the VS/MSDN documentation wrong?
.
Many thanks.