FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Funções "RANDOM" não funcionam!
Posts: 17
Joined: Wed Feb 04, 2009 09:38 PM
Funções "RANDOM" não funcionam!
Posted: Fri Aug 07, 2020 01:41 PM

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

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Funções "RANDOM" não funcionam!
Posted: Fri Aug 07, 2020 01:59 PM

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 (fw): Select all Collapse
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.
Regards



G. N. Rao.

Hyderabad, India
Posts: 17
Joined: Wed Feb 04, 2009 09:38 PM
Re: Funções "RANDOM" não funcionam!
Posted: Fri Aug 07, 2020 02:04 PM

Obrigado!

irei testar.

Posts: 17
Joined: Wed Feb 04, 2009 09:38 PM
Re: Funções "RANDOM" não funcionam!
Posted: Fri Aug 07, 2020 02:18 PM

Para 999999 não funcionou infelizmente pois tem o uso e já testado HB_RandomInt()

:(

Posts: 17
Joined: Wed Feb 04, 2009 09:38 PM
Re: Funções "RANDOM" não funcionam!
Posted: Fri Aug 07, 2020 03:11 PM

Fiz uma pequena mudança e funcionou!

Obrigado meu caro

Continue the discussion