Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Random Number
Message
From
26/11/2001 12:03:27
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Miscellaneous
Thread ID:
00585489
Message ID:
00585799
Views:
28
>>>>>Hi all:
>>>>>
>>>>>How do I get a random number between 1 and 1000 in VFP6
>>>>>I could not find it in help.
>>>>>
>>>>>Thank you.
>>>>
>>>>
? INT(1000 * RAND( ) + 1)
>>>Instead of using 1000 in the equation above, use 999. The +1 could give you 1001, if RAND() returns 1 which it could.
>>
>>I was going to suggest that, but I did a small test
>>
>>
>>do while .T.
>> if ( int( 1000 * rand() + 1 ) > 1000 )
>>   ? "Error"
>>  endif
>>enddo
>>
>>
>>I let it run for 1/2 hour without getting "Error" printed. It is just a possibility that 1001 could be generated, but very unlikely.
>>
>>could try
>>
>>? round( 999 * rand() + 1, 0 )
>>
>
>I have a scheduler type program that was running on multiple PC's for years under FPD 2.6a w/o any errors. After PC's were upgraded the program would generate an error from time to time on one PC only. Further investigation showed that it was a result of RAND() function returning value 1.0. The bottom line, RAND() function can return value 1.0 :)

I thought it would be possible, which is why I gave the above suggestion, using round(), rather than int() - to get the full range. If int() were used (with 999 multiplier), then 1000 wouldn't occur with equal probability of other numbers.
Len Speed
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform