add a number on Random

add a number on Random

Postby Silvio.Falconi » Sat Mar 26, 2022 10:23 am

Image

I have to extract the 90 numbers, one at a time, the random ones. Whenever I extract a number, it is necessary to check if it already exists in a list array and in an atemp array.

I created the function OneNumb(oSay) but I saw that after about 63 numbers the procedure freezes and crashes, once I got to 82 numbers extracted and then the procedure crashed, I don't understand where the error is.
I add the get memo only for demo

Code: Select all  Expand view


#include "fivewin.ch"
static alista

Function test()
local oBtnNum,oBtnShow,oSay, cNumbers:= SPACE(400)
alista := {}
Define Dialog oDlg    SIZE 500,300  PIXEL TRUEPIXEL RESIZABLE

     @ 100,10 BUTTON oBtnNum PROMPT "+" of oDlg  SIZE 40,22 ACTION OneNumb(oSay)
     @ 100,10 BUTTON oBtnShow PROMPT "Show" of oDlg  SIZE 80,22 ACTION ShowList(alista)
     @ 10,10  GET oSay VAR cNumbers MEMO  size 300,200 PIXEL of oDlg

 oDlg:bResized  := <||
          local oRect               := oDlg:GetCliRect()
                oBtnNum:nLeft       := oRect:nRight - 200
                oBtnNum:nTop        := oRect:nBottom - 25
                oBtnShow:nLeft      := oRect:nRight - 100
                oBtnShow:nTop       := oRect:nBottom - 25
                oSay:nTop           := oRect:nTop
                oSay:nLeft          := oRect:nLeft
                oSay:nWidth          := oRect:nRight - 200
                oSay:nBottom        := oRect:nBottom - 40

        return nil
       >


Activate dialog oDlg center ;
          ON INIT ( EVAL( oDlg:bResized))
return nil



// take a number
Function OneNumb(oSay)
     local n := 0, nLen := 1, nRandom, atemp := {}
     local ctxt:=""
  While n < nLen
     nRandom := HB_RandomInt(90)
    if AScan( alista, ltrim(str(nRandom)) ) == 0
     if AScan( atemp, nRandom ) == 0
        n += 1
        aadd( atemp, nRandom )
     endif
   Endif
Enddo


      //add number on alista
     For n= 1 to Len(atemp)
         idcheck:= atemp[n]
         aadd(aLista,ltrim(str(idcheck)))
      next

      //only for demo
        For n= 1 to Len(aLista)
           ctxt+= aLista[n]+"-"
           next
        oSay:settext(ctxt)
     return nil
//--------------------------------------------------------//
  Function ShowList(alista)
     xbrowser alista

   return nil
//------------------------------------------------------//



 
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: add a number on Random

Postby cmsoft » Sat Mar 26, 2022 12:51 pm

Hola Silvio, no se si interpreto bien lo que necesitas. Sacar números hasta el 90 en orden aleatorio y guardarlos en una matriz sin repetidos, e ir mostrándolos en un oSay.
Code: Select all  Expand view
#include "fivewin.ch"
static alista, alistan

Function test()
local oBtnNum,oBtnShow,oSay, cNumbers:= "", oDlg
alista := {}
alistan := {}
Define Dialog oDlg    SIZE 500,300  PIXEL TRUEPIXEL RESIZABLE

     @ 100,10 BUTTON oBtnNum PROMPT "+" of oDlg  SIZE 40,22 ACTION OneNumb(oSay)
     @ 100,10 BUTTON oBtnShow PROMPT "Show" of oDlg  SIZE 80,22 ACTION ShowList()
     @ 10,10  GET oSay VAR cNumbers MEMO  size 300,200 PIXEL of oDlg

 oDlg:bResized  := <||
          local oRect               := oDlg:GetCliRect()
                oBtnNum:nLeft       := oRect:nRight - 200
                oBtnNum:nTop        := oRect:nBottom - 25
                oBtnShow:nLeft      := oRect:nRight - 100
                oBtnShow:nTop       := oRect:nBottom - 25
                oSay:nTop           := oRect:nTop
                oSay:nLeft          := oRect:nLeft
                oSay:nWidth          := oRect:nRight - 200
                oSay:nBottom        := oRect:nBottom - 40

        return nil
       >


Activate dialog oDlg center ;
          ON INIT ( EVAL( oDlg:bResized))
return nil



// take a number
Function OneNumb(oSay)
local nRandom, atemp := {}
local ctxt:=oSay:GetText()
if len(aLista) >= 90
   Msginfo("Lista llena")
   return nil
endif  
While .t.
    nRandom := HB_RandomInt(90)
    if AScan( alistan, nRandom ) == 0    
        aadd( alista, ltrim(str(nRandom)) )
        aadd( alistan, nRandom )
        exit
     endif  
Enddo

oSay:settext(ctxt+ltrim(str(nRandom))+"-")
return nil
//--------------------------------------------------------//
Function ShowList()
     xbrowser alista

return nil
//------------------------------------------------------//
User avatar
cmsoft
 
Posts: 1285
Joined: Wed Nov 16, 2005 9:14 pm
Location: Mercedes - Bs As. Argentina

Re: add a number on Random

Postby Detlef » Sat Mar 26, 2022 4:48 pm

Hi silvio,

here an other approach to get your numbers.
Sorry, cmsoft- didn't want to correct your version... was just too late with my version.

Code: Select all  Expand view
#include "fivewin.ch"

#DEFINE _MIN  1     // not sure if 0 also allowed?
#DEFINE _MAX 90

STATIC aLista   := {}
STATIC aStorage := {}

FUNCTION Main()
//-------------
LOCAL oDlg, oBtnNum, oBtnShow, oSay, cNumbers := SPACE( 400 )

   FillList() // initiate a storage list with possdible numbers

   DEFINE Dialog oDlg SIZE 500,300  PIXEL TRUEPIXEL RESIZABLE

     @ 100,10 BUTTON oBtnNum  PROMPT "+"    of oDlg  SIZE 40,22 ACTION OneNumb( oSay )
     @ 100,10 BUTTON oBtnShow PROMPT "Show" of oDlg  SIZE 80,22 ACTION ShowList( aLista )
     @ 10,10  GET oSay VAR cNumbers MEMO  size 300,200 PIXEL of oDlg

    oDlg:bResized  := <||
          LOCAL oRect               := oDlg:GetCliRect()
                oBtnNum:nLeft       := oRect:nRight - 200
                oBtnNum:nTop        := oRect:nBottom - 25
                oBtnShow:nLeft      := oRect:nRight - 100
                oBtnShow:nTop       := oRect:nBottom - 25
                oSay:nTop           := oRect:nTop
                oSay:nLeft          := oRect:nLeft
                oSay:nWidth         := oRect:nRight - 200
                oSay:nBottom        := oRect:nBottom - 40

        RETURN NIL
       >

   ACTIVATE DIALOG oDlg CENTER ;
          ON INIT ( EVAL( oDlg:bResized ) )
RETURN NIL

// take a number
FUNCTION OneNumb( oSay )
//----------------------
LOCAL nRand
LOCAL cTxt:=""


   if len( aStorage ) == 0
      msgInfo( "List ist full! " )
      RETURN NIL
   endif

   // find a random number
   nRand := HB_RandomInt( _MIN, len( aStorage ) )

   // put it into list
   aadd( aLista, alltrim( str( aStorage[ nRand ], 2, 0 ) ) )

   // this number will be no more available
   adel( aStorage, nRand, .t. )
   asize( aStorage, len( aStorage ) - 1 )

   //only for demo
   for n = 1 to Len( aLista )
      cTxt += aLista[ n ] + "-"
   next

   oSay:setText( cTxt )
RETURN NIL

STATIC PROCEDURE ShowList( aLista )
//---------------------------------
     xbrowser aLista

RETURN

STATIC PROCEDURE FillList()
//-------------------------
LOCAL n

   for n := _MIN to _MAX
      aadd( aStorage, n )
   next
RETURN
 
User avatar
Detlef
 
Posts: 207
Joined: Mon Feb 07, 2022 9:54 pm

Re: add a number on Random

Postby nageswaragunupudi » Sat Mar 26, 2022 5:09 pm

If you want an array of 90 random numbers without repetition created in one go, this is a small sample.
Code: Select all  Expand view
#include "fivewin.ch"

function Main()

   local aNums := ARRAY( 90 )

   AEval( aNums, { |u,i| aNums[ i ] := i } )
   ASort( aNums,,, { |x,y| HB_RandomInt( 0,1 ) == 0 } )

   ? FW_ArrayAsList( aNums, "-" )

return nil
 


Image
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10619
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: add a number on Random

Postby nageswaragunupudi » Sat Mar 26, 2022 5:32 pm

If you want to add one non-repetitive random number each time when you press a button, there are different ways to do it. Here is one way.
Code: Select all  Expand view
#include "fivewin.ch"

Function Main()

   local oDlg,oBtnNum,oBtnShow,oSay, nAt, cNumbers:= ""
   local alista := {}
   local aNatural := Array( 90 )

   AEval( aNatural, { |u,i| aNatural[ i ] := i } )

   Define Dialog oDlg SIZE 500,300  PIXEL TRUEPIXEL RESIZABLE

     @ 100,10 BUTTON oBtnNum PROMPT "+" of oDlg  SIZE 40,22 ;
       WHEN Len( aNatural ) > 0 ;
       ACTION ( nAt := HB_RandomInt( 1, Len( aNatural ) ), ;
                AAdd( aLista, aNatural[ nAt ] ), ;
                HB_ADel( aNatural, nAt, .t. ), ;
                cNumbers := FW_ArrayAsList( aLista, "-" ),;
                oSay:Refresh() )

     @ 10,10 GET oSay VAR cNumbers MEMO  size 300,200 PIXEL of oDlg

     oDlg:bResized  := <||
          local oRect               := oDlg:GetCliRect()
          oBtnNum:nLeft       := oRect:nRight - 200
          oBtnNum:nTop        := oRect:nBottom - 25
          //oBtnShow:nLeft      := oRect:nRight - 100
          //oBtnShow:nTop       := oRect:nBottom - 25
          oSay:nTop           := oRect:nTop
          oSay:nLeft          := oRect:nLeft
          oSay:nWidth          := oRect:nRight - 200
          oSay:nBottom        := oRect:nBottom - 40

        return nil
       >
   Activate dialog oDlg center ;
          ON INIT ( EVAL( oDlg:bResized))

return nil
 


Image
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10619
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: add a number on Random

Postby Detlef » Sat Mar 26, 2022 6:26 pm

Wow! That's very smart! :D
User avatar
Detlef
 
Posts: 207
Joined: Mon Feb 07, 2022 9:54 pm

Re: add a number on Random

Postby nageswaragunupudi » Sat Mar 26, 2022 8:00 pm

This logic is much simpler.
Code: Select all  Expand view
#include "fivewin.ch"

Function Main()

   local oDlg,oBtnNum,oBtnShow,oSay, nAt, cNumbers:= ""
   local alista := {}
   local aSource := Array( 90 )

   AEval( aSource, { |u,i| aSource[ i ] := i } )
   ASort( aSource,,, { |x,y| HB_RandomInt( 0,1 ) == 0 } )

   Define Dialog oDlg SIZE 500,300  PIXEL TRUEPIXEL RESIZABLE

     @ 100,10 BUTTON oBtnNum PROMPT "+" of oDlg  SIZE 40,22 ;
       WHEN Len( aLista ) < 90 ;
       ACTION ( AAdd( aLista, aSource[ Len( aLista ) + 1 ] ), ;
                cNumbers := FW_ArrayAsList( aLista, "-" ),;
                oSay:Refresh() )

     @ 10,10 GET oSay VAR cNumbers MEMO  size 300,200 PIXEL of oDlg

     oDlg:bResized  := <||
          local oRect               := oDlg:GetCliRect()
          oBtnNum:nLeft       := oRect:nRight - 200
          oBtnNum:nTop        := oRect:nBottom - 25
          //oBtnShow:nLeft      := oRect:nRight - 100
          //oBtnShow:nTop       := oRect:nBottom - 25
          oSay:nTop           := oRect:nTop
          oSay:nLeft          := oRect:nLeft
          oSay:nWidth          := oRect:nRight - 200
          oSay:nBottom        := oRect:nBottom - 40

        return nil
       >

   Activate dialog oDlg center ;
          ON INIT ( EVAL( oDlg:bResized))

return nil
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10619
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: add a number on Random

Postby Silvio.Falconi » Sat Mar 26, 2022 8:04 pm

yes but Here not run ok
I explain you :

I have this dialog

Image


If I press "+" then I have this

Image


I select all the numbers except the one taken randomly, that is, the other way around

I made :

@xRow,xCol ButtonBMP oOpe[10] Prompt "+" size 30,30 PIXEL FLAT OF oDlg TOOLTIP "Aggiungi un numero";
WHEN Len( aNatural ) > 0 ;
ACTION ( nAt := HB_RandomInt( 1, Len( aNatural ) ), ;
AAdd( aLista, aNatural[ nAt ] ), ;
HB_ADel( aNatural, nAt, .t. ), ;
AddOne(oBtnNum,aLista,oSay) )


Function AddOne(oCmb,atemp,oSay)
For n= 1 to Len(atemp)
idcheck:= atemp[n]
oCmb[idcheck]:cargo:=1 // I need to change the cargo of btnbmp
SetStatus( oCmb[idcheck],oSay) //this change the color and update the osay
next
return nil



the problem is : perhaps I not explain good , I use alista array to store the numbers selected by the user
when we press "+" and take a number randomly I have to check if that number already exists in the list array before inserting it
If the number is already in the list I am going to delete it in the checklist () function which I also use for other functions


Code: Select all  Expand view
Function checkList(atemp)
local n
    local nAt
         For n= 1 to Len(atemp)
            nAt :=  ASCAN(alista, { | a | a  == ltrim(str(atemp[n])) } )
            If nAt >0
               adel(alista,nAt,.t.)
            Endif
         Next
     return NIL



Then I do a For / next cycle to activate the bmp button and finally update the list and the writings

Code: Select all  Expand view
For idcheck= 1 to 90
            oCmb[idcheck]:cargo:=1
            SetStatus( oCmb[idcheck],oSay)
     next




For a sample :
If I wish select only the Twins numbers I made


Code: Select all  Expand view
Function Twins(oCmb,oSay)
   local atemp:={}
   atemp:={11, 22, 33, 44, 55, 66, 77, 88}  // this is the twins from 1 to 90

   CheckList( atemp )

   For n= 1 to Len(atemp)
      idcheck:= atemp[n]
        oCmb[idcheck]:cargo:=1                    // I need it to change the cargo
        SetStatus( oCmb[idcheck],oSay)        // I need it to change the color and update the oSay
     next
     return NIL



and this is the result

Image



Now On aLista I have allready {11, 22, 33, 44, 55, 66, 77, 88} and I press "+" because I need anotherr number and I wish the computer give me one
it when take the number must check if there is already on aLista before to colorize it
I hope I explain clear
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: add a number on Random

Postby Silvio.Falconi » Sat Mar 26, 2022 10:47 pm

Perhaps I found a solution thanks to Nages ...lesson


STATIC aNatural

aNatural := Array( 90 )

Function test() // where is the table buttons

AEval( aNatural, { |u,i| aNatural[ i ] := i } )

@xRow,xCol ButtonBMP oOpe[10] Prompt "+" size 30,30 PIXEL FLAT OF oDlg TOOLTIP "Aggiungi un numero";
action OneNumb(oBtnNum, oSay,@aNatural )

.....
Function OneNumb(oCmb, oSay,aNatural )
local atemp := {}
local nAt,nAp
local cNum

//check and del to aNatural the numbers are allready on alista
For n=1 to Len(aLista)
cNum:= alista[n]
nAp:= AScan( aNatural, val(cNum) )
IF nAp>0
HB_ADel( aNatural, nAp, .t. )
Endif
Next

// take a number random
nAt := HB_RandomInt( 1, Len( aNatural ) )
AAdd( aTemp, aNatural[ nAt ] )
HB_ADel( aNatural, nAt, .t. )

//-------------------------
Checklist( atemp ) //check the list ( alista)

// change the table numbers
For n= 1 to Len(atemp)
idcheck:= atemp[n]
oCmb[idcheck]:cargo:=1 // change the cargo on btnbmp
SetStatus( oCmb[idcheck],oSay) // change the colors and osay
next
return NIL
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


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 48 guests