Jump to content
Fivewin Brasil

Gerar 10 dezenas aleatoriamente, sem repetição


marcioe

Recommended Posts

entrei na brincadeira... só pra deixar a funcao padrao e flexivel...

 

#include "fivewin.ch"

Function Main()
Local aNumeros

aNumeros := Sorteia(10,1,99)

XBrowse(aNumeros)

Return


Function Sorteia(nQtd,nMin,nMax)
Local nSorteio
Local aNum:={}

Do While .t.
   nSorteio := HB_RandomInt(nMin,nMax)
   If AScan(aNum,nSorteio) == 0
      AAdd(aNum,nSorteio)
   Endif

   If Len(aNum) == nQtd
      Exit
   Endif
Enddo

aSort(aNum)

Return aNum

 

Link to comment
Share on other sites

  • 2 weeks later...

Este exemplo do JMSilva, não repetiu nada, teste e mostre onde repete.


#Include "Fivewin.ch"
	Function Main()
	   Local nSorteio, aNum := {}
	   While .t.
	      nSorteio := HB_RandomInt( 1, 99 )
	      If AScan(aNum,nSorteio) == 0
	         AAdd( aNum, nSorteio )
	      Endif
	      If Len(aNum) == 10
	         xBrowse( Asort( aNum ) )
	         Exit
	      Endif
	      SysRefresh()
	   Enddo
	Return nil

Link to comment
Share on other sites

Este do EMotta, também me pareceu correto. Não repetiu, teste e mostre quando repete e sempre poste o código para críticas.

#Include "Fivewin.ch"
	Function Main()
	   Local aNumeros := 0
	   aNumeros := Sorteia( 10, 1, 99 )
	   XBrowse( aNumeros )
	Return Nil
	
Function Sorteia( nQtd, nMin, nMax )
	   Local nSorteio, aNum := {}
	   While .t.
	      nSorteio := HB_RandomInt( nMin, nMax )
	      If AScan( aNum, nSorteio ) == 0
	         AAdd( aNum, nSorteio )
	      Endif
	      If Len( aNum ) == nQtd
	         Exit
	      Endif
	      SysRefresh()
	   Enddo
	   aSort( aNum )
	Return aNum


 

Link to comment
Share on other sites

Saindo e limpando tudo

#Include "Fivewin.ch"
	Function Main()
	   Local aNumeros := 0
	   aNumeros := Sorteia( 10, 0, 99 )
	   XBrowse( aNumeros )
	
   // Saindo e limpando tudo
	   aNumeros := 0
	   // FreeResources()
	   SysRefresh()
	   // DbCloseAll()
	   RELEASE All
	   HB_GCALL( .T. )
	   CLEAR MEMORY
	   PostQuitMessage( 0 )
	   __QUIT()
	Return Nil
	
Function Sorteia( nQtd, nMin, nMax )
	   Local nSorteio, aNum := {}
	   While .t.
	      nSorteio := HB_RandomInt( nMin, nMax )
	      If AScan( aNum, nSorteio ) == 0
	         AAdd( aNum, nSorteio )
	      Endif
	      If Len( aNum ) == nQtd
	         Exit
	      Endif
	      SysRefresh()
	   Enddo
	   aSort( aNum )
	Return aNum


 

Link to comment
Share on other sites

function test()
local x, aVetor:=Array(10), cnum1:="", cnum2:=""

for x=1 to 10
 aVetor[x] := Int(hb_Random(1, 15))

 if x>1
  do while VerSeTem(aVetor, x)
     aVetor[x] := Int(hb_Random(1, 15))
  enddo
 endif
next

for x=1 to 10
   cnum1 = cnum1 + alltrim(strzero(aVetor[x],2))+" "
next

aVetor := aSort(aVetor)

for x=1 to 10
  cnum2 = cnum2 + alltrim(strzero(aVetor[x],2))+" "
next

? cnum1+CRLF+cnum2

return nil

function VerSeTem(aVetor, x)
local lexist:=.f., y

for y=1 to x-1
 If aVetor[y] = aVetor[x]
   lexist:=.t.
 endif
next
return lexist

será possível rapaz... que coisa difícil (detalhe total de 15  numeros e sorteando 10 pro teste ficar mais forte)

ver ai

Link to comment
Share on other sites

Marcio, o código do JMSILVA e o que eu peguei dele e criei uma função genérica, é impossível repetir... Como o João disse, poste o código que fez repetir...

 

 

 

bom dia a todos, 

 

Apos vários testes ainda continua repetindo dezenas

 

Link to comment
Share on other sites

 

Marcio, o código do JMSILVA e o que eu peguei dele e criei uma função genérica, é impossível repetir... Como o João disse, poste o código que fez repetir...

 

 

 

talvez ele tenha usado o primeiro código que postei, realmente estava repetindo... coloquei um código novo,,,,   vc tb poderá usar o código do JMSILVA que deve ta certo

Link to comment
Share on other sites

Marcio, com certeza teve algum erro da sua parte. Executei o código abaixo que de 100 números ele sorteia 99 e executei algumas vezes e NENHUMA repetiu.... 

 

#include "fivewin.ch"

Function Main()
Local aNumeros

aNumeros := Sorteia(99,1,100)

XBrowse(aNumeros)

Return


Function Sorteia(nQtd,nMin,nMax)
Local nSorteio
Local aNum:={}

Do While .t.
   nSorteio := HB_RandomInt(nMin,nMax)
   If AScan(aNum,nSorteio) == 0
      AAdd(aNum,nSorteio)
   Endif

   If Len(aNum) == nQtd
      Exit
   Endif
Enddo

aSort(aNum)

Return aNum

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...