marcioe Posted March 22, 2018 Report Share Posted March 22, 2018 Olá amigos, Preciso Gerar 10 dezenas aleatoriamente, sem repetição. De 00. A 99 Porém não repetir número Exemplo 01 05 10 43 34 22 78 87 45 94 Quote Link to comment Share on other sites More sharing options...
sistem Posted March 23, 2018 Report Share Posted March 23, 2018 codigo removido pois estava com erro e tem outro codigo bem melhor abaixo Quote Link to comment Share on other sites More sharing options...
marcioe Posted March 23, 2018 Author Report Share Posted March 23, 2018 obrigado amigo, vou testar, agradeço Quote Link to comment Share on other sites More sharing options...
Jmsilva Posted March 24, 2018 Report Share Posted March 24, 2018 Veja este! #include "fivewin.ch" Function Main() Local nSorteio Local aNum:={} Do While .t. nSorteio := HB_RandomInt(1,99) If AScan(aNum,nSorteio) == 0 AAdd(aNum,nSorteio) Endif If Len(aNum) == 10 XBrowse(aNum) Exit Endif Enddo Return nil Theotokos 1 Quote Link to comment Share on other sites More sharing options...
emotta Posted March 26, 2018 Report Share Posted March 26, 2018 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 frkiko 1 Quote Link to comment Share on other sites More sharing options...
marcioe Posted March 27, 2018 Author Report Share Posted March 27, 2018 obrigado a todos Quote Link to comment Share on other sites More sharing options...
marcioe Posted April 11, 2018 Author Report Share Posted April 11, 2018 bom dia a todos, Apos vários testes ainda continua repetindo dezenas Quote Link to comment Share on other sites More sharing options...
kapiaba Posted April 11, 2018 Report Share Posted April 11, 2018 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 Quote Link to comment Share on other sites More sharing options...
kapiaba Posted April 11, 2018 Report Share Posted April 11, 2018 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 Quote Link to comment Share on other sites More sharing options...
kapiaba Posted April 11, 2018 Report Share Posted April 11, 2018 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 frkiko 1 Quote Link to comment Share on other sites More sharing options...
cnavarro Posted April 11, 2018 Report Share Posted April 11, 2018 Lo que puede repetir es una decena de las diez que ha de hacer Quote Link to comment Share on other sites More sharing options...
sistem Posted April 11, 2018 Report Share Posted April 11, 2018 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 Quote Link to comment Share on other sites More sharing options...
emotta Posted April 11, 2018 Report Share Posted April 11, 2018 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 Quote Link to comment Share on other sites More sharing options...
sistem Posted April 11, 2018 Report Share Posted April 11, 2018 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 Quote Link to comment Share on other sites More sharing options...
emotta Posted April 11, 2018 Report Share Posted April 11, 2018 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 Quote Link to comment Share on other sites More sharing options...
marcioe Posted April 11, 2018 Author Report Share Posted April 11, 2018 Blz amigos, vou fazer o teste Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.