nRandom Numbers no duplicates

nRandom Numbers no duplicates

Postby Silvio.Falconi » Tue Dec 07, 2021 1:07 pm

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)


Image

I tried but I get duplicates what should I do?
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm

Re: nRandom Numbers no duplicates

Postby karinha » Tue Dec 07, 2021 3:41 pm

Mira se ayuda:

Code: Select all  Expand view

// \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
 
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7794
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: nRandom Numbers no duplicates

Postby karinha » Tue Dec 07, 2021 3:48 pm

Code: Select all  Expand view

// \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
 
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7794
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: nRandom Numbers no duplicates

Postby Marcelo Via Giglio » Tue Dec 07, 2021 6:11 pm

Code: Select all  Expand view

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
 
Marcelo Via Giglio
 
Posts: 1064
Joined: Fri Oct 07, 2005 3:33 pm
Location: Cochabamba - Bolivia

Re: nRandom Numbers no duplicates

Postby FranciscoA » Tue Dec 07, 2021 6:25 pm

Code: Select all  Expand view
//------------------------------------------------//
// 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
 
Francisco J. Alegría P.
Chinandega, Nicaragua.

Fwxh-MySql-TMySql
User avatar
FranciscoA
 
Posts: 2158
Joined: Fri Jul 18, 2008 1:24 am
Location: Chinandega, Nicaragua, C.A.


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 58 guests