Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Random number generation
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00785329
Message ID:
00785355
Views:
10
Thanks for your input and your object oriented deck of cards class.

Jeff

>Looks random enough.
>A slightly more effitient [won't get duplicates] from a card suffling class I made. The getRandomOneTo function will return a random number from 1 to n. In the shuffledeck method the for loop decrements the GetRandomOneTo from 52 down, thereby not generating dups. In your case, you'd use the number of active employees of course.
>
>Best of luck.
>
>Define Class aCards As aRandom
> oDeck = .Null.
> *%
> *% Method: Aclasschance.aCards.oDeck_Access
> *% Purpose: Access method for oDeck property. Creates the deck as required
> *%
> Protected Function oDeck_Access As cDeck
> If Vartype(This.oDeck) <> 'O'
> This.oDeck = Createobject('cDeck')
> This.oDeck.PopulateDeck()
> Endif
> Return This.oDeck
> Endfunc
> *%
> *% Method: Aclasschance.aCards.ShuffleDeck
> *% Purpose: Shuffle the deck.
> *%
> Function ShuffleDeck As Void
> Local loShuffeledDeck,lnCount,lnIndex
> loShuffledDeck = Createobject('cDeck')
> For lnCount = 52 To 1 Step -1
> lnIndex = This.GetRandomOneTo(lnCount)
> loShuffledDeck.Add(This.oDeck.Item(lnIndex), ;
> Padl(Alltrim(Str(lnCount)),2,'0'))
> This.oDeck.Remove(lnIndex)
> Endfor
> This.oDeck = loShuffledDeck
> Store .F. To loShuffeledDeck
> Endfunc
>Enddefine
>
>Define Class aRandom As Custom
> Protected lSeeded
> lSeeded = .F.
>
> Protected Function GetRandomNum As Number
> Local lnRetVal
> If !This.lSeeded
> lnRetVal = Rand(-1)
> Else
> lnRetVal = Rand()
> Endif
> This.lSeeded = .T.
> Return lnRetVal
> Endfunc
>
> Function GetRandomOneTo(lnEnd As Number) As Integer
> *% Purpose: Returns a random integer from one to lnEnd
> *%
> *% Parameters: lnEnd: Last number in the series. ie. 10 gives number between 1 & 10
> Return Int(((This.GetRandomNum()*10)*(lnEnd/10))+1)
> Endfunc
>Enddefine
>
>
>
>
>>Hello all,
>>
>>I have a program that selects employees for random drug testing. I would like to verify that my code produces the most random number possible. I just pasted the code below, any comments are appreciated.
>>
>>
>>Thanks, Jeff
>>
>>_________________________________________________________________________
>>

>>lparameters lcPlantNo
>>
>>* get only active employee #'s into the temporary
>>* table
>>select prempl,first,last ;
>> from prempl01 where left(prempl,1) = lcPlantNo ;
>> and status = "A" ;
>>union all ;
>>select prempl,first,last ;
>> from prempl02 where left(prempl,1) = lcPlantNo ;
>> and status = "A" ;
>> into cursor tmpEmpl
>>
>>select tmpEmpl
>>
>>nRecs = reccount()
>>* the number of tests to perform is 5% of
>>* the total population for this plant
>>nNumTests = round(nRecs * .05,0)
>>nTested=0
>>* initialize the seed value
>>nEmplNo = rand(-1)
>>do while nTested < nNumTests
>>
>> * get the random employee number
>> nEmplNo = int(nRecs * rand() + 1)
>> * random sort for printout
>> nRandSort = rand()*100
>>
>> if nEmplNo < 1 or nEmplNo > nRecs
>> loop
>> endif
>>
>> * save the employee to the temp file
>> goto nEmplNo in tmpEmpl
>> select testnumbers
>> seek tmpEmpl.prempl
>> if !found()
>> * employee not already included in this test
>> nTested = nTested + 1
>> append blank
>> replace empid with tmpEmpl.prempl
>> replace last with tmpEmpl.last
>> replace first with tmpEmpl.first
>> replace randsort with nRandSort
>> endif
>>enddo
>>
>>use in tmpEmpl
Previous
Reply
Map
View

Click here to load this message in the networking platform