Page 1 of 1

Funções "RANDOM" não funcionam!

PostPosted: Fri Aug 07, 2020 1:41 pm
by Ladinilson
Fiz vários testes com nRANDOM(), RANDOM(), HB_RandomInt(), etc e todas elas NÃO FUNCIONARAM nas suas totalidades ou seja, todas deixaram de fornecer alguns números em uma de range 999999 e estou com muita dificuldade em encontrar uma solução para isso já que em se tratando de sorteios, nenhum número pode faltar ou se repetir.

Algum de vocês tem alguma rotina própria criada para esta função que eu consiga testar?

Obrigado

Re: Funções "RANDOM" não funcionam!

PostPosted: Fri Aug 07, 2020 1:59 pm
by nageswaragunupudi
todas elas NÃO FUNCIONARAM nas suas totalidades

Your expectation is wrong.
All the above functions do what they are expected to do.
We can not expect that random number generators generate "every" number in a series. Also they may generate duplicates.

It is for us (the programmer) to use the random function to get what we want.

For example, if we want a function to generate every number between 1 to 999 without repeating then we can try this approach.

Code: Select all  Expand view

function RandomList

   local aList    := Array( 999 )
   local nAt, aRandom := {}

   AEval( aList, { |u,i| aList[ i ] := i } )

   do while Len( aList ) > 0
      nAt      := HB_RandomInt( 1, Len( aList ) )
      AAdd( aRandom, aList[ nAt ] )
      ADel( aList, nAt, .t. )
   enddo

   XBROWSER aRandom SHOW RECID

return nil
 


This function gives a list of randomly generated list of numbers from 1 to 999 without repeating and covering all numbers.

Re: Funções "RANDOM" não funcionam!

PostPosted: Fri Aug 07, 2020 2:04 pm
by Ladinilson
Obrigado!

irei testar.

Re: Funções "RANDOM" não funcionam!

PostPosted: Fri Aug 07, 2020 2:18 pm
by Ladinilson
Para 999999 não funcionou infelizmente pois tem o uso e já testado HB_RandomInt()

:(

Re: Funções "RANDOM" não funcionam!

PostPosted: Fri Aug 07, 2020 3:11 pm
by Ladinilson
Fiz uma pequena mudança e funcionou!

Obrigado meu caro