Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Random Number
Message
From
27/11/2001 09:54:46
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Miscellaneous
Thread ID:
00585489
Message ID:
00586290
Views:
36
>>>>>>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.
>>>
>>>Right. Than the code that generates random integers in the specified range would be
>>>lnRandMin = 1
>>>lnRandMax = 1000
>>>
>>>? INT((lnRandMax - lnRandMin) * RAND( ) + lnRandMin)
>>>
Correct?
>>
>>I think you should use round() rather than int(), see my other replies,
>Is this ok with you? :)
lnRandMin = 1
>lnRandMax = 1000
>
>? RAND((lnRandMax - lnRandMin) * RAND( ) + lnRandMin,0)
>
>
>> though I'm not convinced rand() returns 1.0
>
>I know it does, see my other post in this thread. :)

Try the following :
do while .T.
 rv = rand()
 if rv = 0.0
   ? rv
 endif
 if rv = 1.0
   ? rv
 endif
enddo
I've left this running for several hours now without 1 being printed but lots of 0's (VFP6), so empirically I'm not convinced - you say it does & the manual says between 0 & 1, so I will accept than as a possibility without, hence my variation using round() rather than int(), which avoids the problem.

Using int() will mean that the maximum value will only be returned when rand() returns 1.0 exactly, whereas using round() at least gives a range of values for which 1 is returned, though this still gives low weighting to the return of 1 & 1000.
Len Speed
Previous
Reply
Map
View

Click here to load this message in the networking platform