Moderador: Moderadores
function Ran( nLimite )
static nGuarda:= 1
local I
local nResult
if ValType( nLimite ) # [N]
nLimite:= 100
endif
I:= Seconds()
while nLimite > I
I:= I * 100 +Seconds()
enddo
nGuarda:= (nGuarda +I) / (nResult:= nGuarda * I % nLimite +1)
nResult:= Int( nResult )
return nResult
cRANDOM := ALLTRIM(STR(RAN(9999)))
fINDEX := xTEMP+"TEMP" + cRANDOM
func RND && ( Min, Max )
* Esta funcao gera um numero aleatorio entre min e max (inclusive).
*
* Returns a floating-point pseudo-random number (PRN) between 0 and maxval
* (left-inclusive), with maxval defaulting to 1. If maxval is in the same
* order of size as the generator's period, the distribution of the
* resulting PRNs will be irregular, but since PERIOD is 2147483647, that
* should not be a problem in practice; people doing astrophysics or monte
* carlo simulation using Clipper deserve everything they get, anyway .
*
* Adaptada para o Harbour com 2 parametros: limite min e maximo,
* mantendo maxval sempre 1.
* Sem os parametros min e max retorna o random como foi gerado.
*
* Esta funcao nao funciona se RND_SEED nao puder ir se modificando a cada chamada.
* Portanto, um executavel gerando um unico PRN e sendo chamado varias vezes em um
* arquivo BAT, por exemplo, irá gerar uma distruibuicao irregular que em nada se
* parecerá com numeros aleatorios.
*
* Para gerar numeros aleatorios seguidamente, use esta funcao seguidamente no mesmo
* executavel.
*
para min,max
private nRandom,nmaxval,range
IF TYPE([RND_PERIOD])#[N]
PUBLIC RND_PERIOD,RND_FACTOR,RND_SEED
RND_PERIOD=2147483647
RND_FACTOR=16807
rnd_seed = 0
ENDIF
do while rnd_Seed = 0
rnd_Seed = Seconds()
rnd_Seed = ( rnd_Seed * 12345 ) % RND_PERIOD
enddo
rnd_Seed = RND_FACTOR * rnd_Seed - Int(RND_FACTOR * rnd_Seed / RND_PERIOD ) * RND_PERIOD
nRandom = (rnd_Seed / RND_PERIOD)
if pcount()=2
range=max-min+1
nrandom = int(nrandom*range+min)
endif
RETURN nRandom
Usuários vendo este fórum: Google [Bot] e 6 visitantes