Int() rounds towards negative infinity.
Rnd() returns a number from 0 to 1 inclusive. At least this is how I documented it in my book, and how I understand the online help. I have not checked the source code to see what it does.


Johnny Andersson wrote:

I found that the Rnd function returns values (0.00000... to 1.00000...), not (0.00000... to 0.99999...), which would be better in most cases.

Example:
Dim MyInteger As Integer
Dim MyArray(1 To 6) As Integer

Randomize
MyInteger=Int(6*Rnd)+1 'Supposed to return numbers 1 to 6, byt may now and then return 7. Happened to me many times.
MyArray(MyInteger)=MyArray(MyInteger)+1 'Causes an error message if MyInteger>6 which may happen in this case.


In this example Rnd returns 1.

Sub RndTest
    Randomize 30091
    Print Rnd
End Sub


This example took about 2 minutes to run on my PC (450 MHz P2, a few other applications were running at the same time):


Sub RndTest0
Dim i As Long
Dim Count1 As Integer, Count2 As Integer
Dim Value As Double
Dim ExecTime As Long
ExecTime=Timer
Randomize
For i=1 To 1000000
Value=Rnd
If Value>0.99999 Then
Count1=Count1+1
If Value=1 Then
Count2=Count2+1
Endif
EndIf
Next i
ExecTime=Timer-ExecTime
Print "Rnd>0.99999 occured "+Count1+" times, Rnd=1 occured "+Count2+" times. This was calculated in "+ExecTime+" s."
End Sub


It seems like every time Rnd>0.99999 then Rnd=1, so even though Rnd produces 15 decimals it seems like only 5 (or less) are relevant. When I tried Rnd=1 at 20 to 34 occasions (different result different times of course) which indicates that Rnd=1 about once every 29000 to 50000 times.

Anyway, is Rnd=1 supposed to ever happen or is this a bug?


--
Andrew Pitonyak
My Macro Document: http://www.pitonyak.org/AndrewMacro.sxw
My Macro Book: http://www.hentzenwerke.com/catalog/oome.htm
Free Info:  http://www.pitonyak.org/oo.php


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to