Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
tools
vsnet.act
vsnet.debugging
vsnet.documentation
vsnet.enterprise.tools
vsnet.general
vsnet.ide
vsnet.jlca
vsnet.servicepacks
vsnet.setup
vsnet.vsip
vsnet.vss
vsnet.vstools.office
vstudio.development
vstudio.extensibility
vstudio.general
vstudio.helpauthoring
vstudio.setup
vstudio.sourcesafe
  
 
date: Fri, 14 Mar 2008 09:40:03 -0700,    group: microsoft.public.vsnet.documentation        back       


MS Bug or MSDN/Documentation typo???   
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.
date: Fri, 14 Mar 2008 09:40:03 -0700   author:   Smudge

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us