FoxPro唯一随机数

时间:2016-09-21 13:56:41

标签: visual-foxpro foxpro

可能有人帮忙,寻找唯一随机生成的数字。 目前有4个酒吧,如果数据有超过4个酒吧,需要随机选择酒吧并将值放在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 

如果有什么不可理解的,请告诉我。

1 个答案:

答案 0 :(得分:0)

你正在使用int(rand()* 10)+ 1.无论是VFP还是任何其他语言,你的目的是选择1到10之间的随机数。你的代码中没有任何内容表明它应该是唯一的Pub1 ,Pub2,...当您从最大值10中选择4个值时,重复调用rand可能会返回相同的值是正常的。相反,您可以执行类似这样的操作(未经测试从头顶开始):

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

请注意,选择A,选择B样式编码很危险。改为使用别名。