I have to calculate a random number up to 1 10
I have to extract 1 at a time for 10 times and I don't have to extract a number that has already been extracted, what should I do?
num:=nRandom(10)
I tried but I get duplicates what should I do?
// \samples\RANDOM1.PRG
#Include "FiveWin.ch"
FUNCTION RandomList()
LOCAL aList := ARRAY( 100 ) // 999999
LOCAL nAt, aRandom := {}
AEval( aList, { | u, i | aList[ i ] := i } )
WHILE LEN( aList ) > 0
SYSREFRESH()
nAt := HB_RandomInt( 1, LEN( aList ) )
AADD( aRandom, aList[ nAt ] )
ADEL( aList, nAt, .T. )
ENDDO
XBROWSER aRandom SHOW RECID
RETURN NIL
// FIN / END
// \samples\RANDOM2.PRG
#include "FiveWin.ch"
STATIC aNumeros
FUNCTION RandomList()
LOCAL cTexto := ""
LOCAL cFile := "numeros.txt"
LOCAL nSort
// aqui inicia o sorteio considerando o range de 1 até 100
MySorteio( .T., 10 )
WHILE( nSort := MySorteio() ) <> 0
SYSREFRESH()
cTexto += AllTrim( Str( nSort ) ) + Chr( 13 ) + Chr( 10 )
ENDDO
MemoWrit( cFile, cTexto )
WinExec( "notepad " + cFile )
RETURN NIL
FUNCTION MySorteio( lInit, nTotal )
LOCAL nI
LOCAL nRet := nil
LOCAL nNum
LOCAL nSort := 0
IF lInit == nil
lInit := .F.
ENDIF
IF lInit // inicia array com numeros a serem sorteados
aNumeros := Array( nTotal )
FOR nI := 1 TO Len( aNumeros )
aNumeros[ nI ] := nI
NEXT
ELSE
IF ValType( aNumeros ) == "A" .AND. Len( aNumeros ) > 0
nNum := hb_RandomInt( Len( aNumeros ) )
nSort := aNumeros[ nNum ]
ADel( aNumeros, nNum, .T. )
ENDIF
ENDIF
RETURN( nSort )
// FIN / END
FUNCTION RandomList( nNum )
LOCAL aList := ARRAY( nNum )
LOCAL nAt, aRandom := {}
AEval( aList, { | u, i | aList[ i ] := 0 } )
DO WHILE LEN( aRandom ) < nNum
nAt := HB_RandomInt( 1, nNum )
DO WHILE aList[ nAt ] != 0
nAt := HB_RandomInt( 1, nNum )
ENDDO
aList[ nAt ] := 1
AADD( aRandom, nAt )
ENDDO
XBROWSER aRandom SHOW RECID
RETURN NIL
//------------------------------------------------//
// EXTRAER NUMEROS RANDOM SIN DUPLICADOS
//------------------------------------------------//
Function NumRandomNoDuplic()
local n := 0, nLen := 10, nRandom, aRandom := {}
While n < nLen
nRandom := HB_RandomInt(nLen)
if AScan( aRandom, nRandom ) == 0
n += 1
aadd( aRandom, nRandom )
endif
Enddo
XBROWSER aRandom TITLE "NUMEROS RANDOM SIN DUPLICADOS" ;
SHOW SLNUM ;
SETUP ( oBrw:cHeaders := {"RANDOM"}, oBrw:nHeadStrAligns := AL_CENTER, oBrw:nDataStrAligns := AL_CENTER )
Return nil
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: No registered users and 40 guests