0
votes

Could some one help, Looking for Unique random generated number. Currently have 4 pubs, If data have more than 4 pubs, need randomly select the pubs and place the value to Pub1 Pub2 Pub3 and Pub4 fields. Can't find solution by using FoxPro.

SELE A
USE TEST
REPL PUB1 WITH "" ALL
REPL PUB2 WITH "" ALL
REPL PUB3 WITH "" ALL
REPL PUB4 WITH "" ALL
REPL RANDOM1 WITH "" ALL
REPL RANDOM2 WITH "" ALL
REPL RANDOM3 WITH "" ALL
REPL RANDOM4 WITH "" ALL
REPL RANDOMLOG WITH "" ALL

SELE B
USE WHATPUB

SELE A
GO TOP
DO WHILE !EOF()
    cBRANCH=BRANCH
    SELE B
    SET FILTER TO BRANCH=cBRANCH
    COUN TO nBRANCHQTY

    IF nBRANCHQTY<=4
        FOR loop=1 TO nBRANCHQTY
            SELE B
            LOCA FOR loop=FT_URN
            IF FOUND()
                cPUBID=PUBID
                SELE A
                cFLD1="PUB"+LTRIM(STR(loop))
                REPL (cFLD1) WITH cPUBID
            ENDIF
        NEXT loop

    ELSE

        SELE A


        FOR loop=1 TO 4
            SELE A



            DO WHILE nRANDOMPUB>nBRANCHQTY 
                nRANDOMPUB=INT(RAND()*10)+1     
            ENDDO


            SELE B
            LOCATE FOR nRANDOMPUB=FT_URN

            IF FOUND()
                cPUBID=PUBID
                SELE A
                cFLD1="PUB"+LTRIM(STR(loop))
                cFLD2="RANDOM"+LTRIM(STR(loop))

                REPL (cFLD1) WITH cPUBID
                REPL (cFLD2) WITH LTRIM(STR(nRANDOMPUB))
            ENDIF

            nRANDOMPUB=9999999

        NEXT loop

    ENDIF

    SELE A
    SKIP

ENDDO

GO TOP
BROW FIELDS BRANCH,RANDOMLOG,RANDOM1,PUB1,RANDOM2,PUB2,RANDOM3,PUB3,RANDOM4,PUB4 

If something is not understandable, just let me know.

1
rand() is the random number generator. Seed it with -1 initially to get the maximum distribution. I would revise your code but it is not understandable enough. Should give error. - Cetin Basoz
I have tried code, workig fine, except the fact that I still have dublication in pubs. Like Pub1=4, Pub2=4, Pub3=3, Pub4=8. The main idea is to make all numbers different, and by using FoxPro.. I could not understand how. - user6105633
Please post your revisions. - Missy

1 Answers

0
votes

You are using int(rand() * 10) + 1. Be it VFP or any other language your intent is to select a random number between 1 and 10. There is nothing in your code that says it should be unique Pub1, Pub2, ... It is normal that repeated calls to rand may return same value when you are choosing 4 values out of max 10. Instead you could do something like this (untested off the top of my head):

Create Cursor crsRand (pubVal i)
Local ix
For ix = 1 To 10
    Insert Into crsRand Values (m.ix)
Endfor

Rand(-1)

Select 0
Use TEST
Replace PUB1 With "", ;
    PUB2 With "", ;
    PUB3 With "", ;
    PUB4 With "", ;
    RANDOM1 With "", ;
    RANDOM2 With "", ;
    RANDOM3 With "", ;
    RANDOM4 With "", ;
    RANDOMLOG With "" All

Use WHATPUB In 0

Local cBRANCH, nBranch, nBRANCHQTY, Loop, cPubId, cFld1
Local Array laRandPub[1]

Select TEST
Scan
    cBRANCH=BRANCH
    Select WHATPUB
    Count For BRANCH=m.cBRANCH To nBRANCHQTY

    If m.nBRANCHQTY<=4
        Select WHATPUB
        For Loop=1 To m.nBRANCHQTY
            Locate For FT_URN=m.loop
            If Found()
                cPubId=PUBID
                Select TEST
                cFld1="PUB"+Ltrim(Str(m.loop))
                Replace (m.cFld1) With m.cPubId
            Endif
        Endfor
    Else
        Select pubVal From ;
            (Select Top 4 pubVal, Rand() From crsRand Where pubVal <= m.nBRANCHQTY Order By 2) tmp ;
            into Array laRandPub

        For Loop=1 To 4
            Select WHATPUB
            Locate For FT_URN=laRandPub[m.loop]
            If Found()
                select Test
                cPubId=PUBID
                cFld1="PUB"+Ltrim(Str(m.loop))
                cFLD2="RANDOM"+Ltrim(Str(m.loop))

                Repl (m.cFld1) With m.cPubId, (m.cFLD2) With Ltrim(Str(laRandPub[m.loop]))
            Endif
        Endfor
    Endif
Endscan

Locate
Brow Fields BRANCH,RANDOMLOG,RANDOM1, ;
            PUB1,RANDOM2,PUB2,RANDOM3,PUB3,RANDOM4,PUB4

Note that Select A, Select B style coding is dangerous. Use aliases instead.