FoxPro 唯一随机数

FoxPro Unique Random Number

有人能帮忙吗,寻找唯一随机生成的数字。 目前有4个pubs,如果数据超过4个pubs,需要随机select个pubs并将值赋给Pub1 Pub2 Pub3和Pub4字段。无法使用 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 

如果有什么不明白的地方,请告诉我。

您正在使用 int(rand() * 10) + 1。无论是 VFP 还是任何其他语言,您的意图都是 select 1 到 10 之间的随机数。您的代码中没有任何内容说它应该是唯一的 Pub1,Pub2,...当您从最多 10 个值中选择 4 个值时,重复调用 rand 可能 return 相同的值是正常的。相反,您可以做这样的事情(未经测试我的头顶):

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

请注意,Select A,Select B 样式编码是危险的。请改用别名。