vfp高手请进有个关于随机的问题请教~

来源:百度知道 编辑:UC知道 时间:2024/06/07 01:43:50
怎样让vfp随机产生一个4位的数,是由1-9组成的,这4个数不能有重复的数字,请高手回答,不胜感激~
用rand()么?

可以把下面代码做成函数调用,

tt_tmp=INT((9)*RAND())+1 为随机产生一个1-9的数字,
tt_end为最终产生的四个不同位数的数字.

下面是代码:

tt_tmp=INT((9)*RAND())+1
tt1=tt_tmp

tt_tmp=INT((9)*RAND())+1
do while tt1=tt_tmp
tt_tmp=INT((9)*RAND())+1
enddo
tt2=tt_tmp

tt_tmp=INT((9)*RAND())+1
do while tt1=tt_tmp or tt2=tt_tmp
tt_tmp=INT((9)*RAND())+1
enddo
tt3=tt_tmp

tt_tmp=INT((9)*RAND())+1
do while tt1=tt_tmp or tt2=tt_tmp or tt3=tt_tmp
tt_tmp=INT((9)*RAND())+1
enddo
tt4=tt_tmp
tt_end=tt1+tt2*10+tt3*100+tt4*1000
?tt_end

***本程序在VFP9下测试通过。

local lcResult,lcRand

lcResult = ""
DO WHIL LEN(lcResult)<4 && 产生一个4位的数
lcRand = str(rand()*10,1) &&随机产生一个1位的数
IF lcRand $ lcResult=.F. && 这4个数不能有重复的数字
IF ISDIGIT(lcRand) &&一定要是数字,不能是'*'
lcResult = lcResult + lcRand &&没重复就加上这个数
ENDIF
ENDIF