Jump to content

Random number from arrays?


Recommended Posts

Hey, I was wondering if somebody could helpt me out with my little problem here:

post-18937-1209825691_thumb.jpg

This is going to be part of a eartraining program that im trying to write, anyhow, I have to get a random number that is:

1. part of any column that has been ticked

2. part of any row that has been ticked

3. is part of the required numbers

E.g. If row 3 and 5 are ticked and column 1 and 2 are ticked (neglecting the required numbers field) it should randomly choose between 11, 22, 44, 80.

Is there a way how to do this simply? I cant think of anything except going through 100 if statements which is certainly stupid.

Would be really glad if somebody could help me out.

This is my script so far:

#include <GUIConstants.au3>

Dim $row1data = _ArrayCreate(1,2,3)
Dim $row2data = _ArrayCreate(4,5,6)
Dim $row3data = _ArrayCreate(11,22,33)
Dim $row4data = _ArrayCreate(44,80,90)
Dim $colum1data = _ArrayCreate(1,4,11,44)
Dim $colum1data = _ArrayCreate(2,5,22,80)
Dim $colum1data = _ArrayCreate(3,6,33,90)

#include <Array.au3>

Dim $avArray
$avArray = _ArrayCreate("JPM", "Holger", "Jon", "Larry", "Jeremy", "Valik", "Cyberslug", "Nutster", "Tylo", "JdeB")

$Form2 = GUICreate("Form2", 694, 428, 303, 219)
$Pic1 = GUICtrlCreatePic( @ScriptDir & "\matrix.jpg", 152, 80, 364, 212, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))
GUICtrlSetState(-1, $GUI_DISABLE)
$colum1 = GUICtrlCreateCheckbox("Checkbox1", 240, 88, 13, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$colum2 = GUICtrlCreateCheckbox("Checkbox2", 336, 88, 13, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$colum3 = GUICtrlCreateCheckbox("Checkbox3", 440, 88, 13, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$row1 = GUICtrlCreateCheckbox("Checkbox4", 160, 120, 13, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$row2 = GUICtrlCreateCheckbox("Checkbox5", 160, 160, 13, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$row3 = GUICtrlCreateCheckbox("Checkbox6", 160, 200, 13, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$row4 = GUICtrlCreateCheckbox("Checkbox7", 160, 234, 13, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$Label1 = GUICtrlCreateLabel("Random Pick:", 320, 344, 71, 17)
$Label2 = GUICtrlCreateLabel("0", 416, 320, 68, 64)
GUICtrlSetFont(-1, 33, 400, 0, "MS Sans Serif")
$pickone = GUICtrlCreateButton("Pick One", 120, 336, 75, 25, 0)
$Input1 = GUICtrlCreateInput("1,2,3,4,5,6,7,8,33,44,90,91,92,93,94,95,96", 224, 24, 289, 21)
$Label3 = GUICtrlCreateLabel("Must be on of these numbers:", 64, 24, 144, 17)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $pickone
            Dim $alsorequired = _ArrayCreate(GUICtrlRead($Input1)
            ;now how pick a number out that is: 
            ; 1. part of any colum that has been ticked
            ; 2. part of any row that has been ticked
            ; 3. is part of the required numbers
    EndSwitch
WEnd

Cheers,

Adrian

post-18937-1209825701_thumb.png

Link to comment
Share on other sites

You should use Arrays:

I marked the changes with Arrows :)

; 3. is part of the required numbers -> not done, Must come to this line: $result &= $RowCol[$x][$y] & "|"

#include <GUIConstants.au3>

Dim $RowCol[3][4] = [[1,4,11,44],[2,5,22,80],[3,6,33,90]] ;<<<<<<<<<<<<<<<<

#include <Array.au3>

Dim $avArray
$avArray = _ArrayCreate("JPM", "Holger", "Jon", "Larry", "Jeremy", "Valik", "Cyberslug", "Nutster", "Tylo", "JdeB")

$Form2 = GUICreate("Form2", 694, 428, 303, 219)
;$Pic1 = GUICtrlCreatePic( @ScriptDir & "\matrix.jpg", 152, 80, 364, 212, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))
GUICtrlSetState(-1, $GUI_DISABLE)
Dim $colum[3] ;<<<<<<<<<<<<<<<<
$colum[0] = GUICtrlCreateCheckbox("Checkbox1", 240, 88, 13, 17)
;^^\<<<<<<<<<<<<<<<
GUICtrlSetState(-1, $GUI_CHECKED)
$colum[1] = GUICtrlCreateCheckbox("Checkbox2", 336, 88, 13, 17)
;^^\<<<<<<<<<<<<<<<
GUICtrlSetState(-1, $GUI_CHECKED)
$colum[2] = GUICtrlCreateCheckbox("Checkbox3", 440, 88, 13, 17)
;^^\<<<<<<<<<<<<<<<
GUICtrlSetState(-1, $GUI_CHECKED)
Dim $row[4] ;<<<<<<<<<<<<<<<
$row[0] = GUICtrlCreateCheckbox("Checkbox4", 160, 120, 13, 17)
;^^\<<<<<<<<<<<<<<<
GUICtrlSetState(-1, $GUI_CHECKED)
$row[1] = GUICtrlCreateCheckbox("Checkbox5", 160, 160, 13, 17)
;^^\<<<<<<<<<<<<<<<
GUICtrlSetState(-1, $GUI_CHECKED)
$row[2] = GUICtrlCreateCheckbox("Checkbox6", 160, 200, 13, 17)
;^^\<<<<<<<<<<<<<<<
GUICtrlSetState(-1, $GUI_CHECKED)
$row[3] = GUICtrlCreateCheckbox("Checkbox7", 160, 234, 13, 17)
;^^\<<<<<<<<<<<<<<<
GUICtrlSetState(-1, $GUI_CHECKED)
$Label1 = GUICtrlCreateLabel("Random Pick:", 320, 344, 71, 17)
$Label2 = GUICtrlCreateLabel("0", 416, 320, 68, 64)
GUICtrlSetFont(-1, 33, 400, 0, "MS Sans Serif")
$pickone = GUICtrlCreateButton("Pick One", 120, 336, 75, 25, 0)
$Input1 = GUICtrlCreateInput("1,2,3,4,5,6,7,8,33,44,90,91,92,93,94,95,96", 224, 24, 289, 21)
$Label3 = GUICtrlCreateLabel("Must be on of these numbers:", 64, 24, 144, 17)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $pickone
       $result = ""
            For $x = 0 To 2 ;<<<<<<<<< Because of Array, easy walk-through :)
                If _IsChecked($colum[$x]) Then
                    For $y = 0 To 3 ;<<<<<<<<<
                        If _IsChecked($row[$y]) Then
                            $result &= $RowCol[$x][$y] & "|" ;<<<<<<<<<
                        EndIf
                    Next
                EndIf
            Next
            MsgBox(0, '', $result)
            ;now how pick a number out that is:
            ; 1. part of any colum that has been ticked
            ; 2. part of any row that has been ticked
            ; 3. is part of the required numbers
    EndSwitch
WEnd

Func _IsChecked($control)
    Return BitAnd(GUICtrlRead($control),$GUI_CHECKED) = $GUI_CHECKED
EndFunc
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

That is so cool, thanks! I tried to take your way further but after trying for a few hours I gota ask for help again, I cant get it to work with the next array, one of many reasons is when i convert the $result string to an array i get an extra value.

Here is what I tried:

#include <GUIConstants.au3>
#include <Array.au3>

Dim $RowCol[3][4] = [[1, 4, 11, 44],[2, 5, 22, 80],[3, 6, 33, 90]] ;<<<<<<<<<<<<<<<<

$Form2 = GUICreate("Form2", 694, 428, 303, 219)
$Pic1 = GUICtrlCreatePic(@ScriptDir & "\matrix.jpg", 152, 80, 364, 212, BitOR($SS_NOTIFY, $WS_GROUP, $WS_CLIPSIBLINGS))
GUICtrlSetState(-1, $GUI_DISABLE)
Dim $colum[3] ;<<<<<<<<<<<<<<<<
$colum[0] = GUICtrlCreateCheckbox("Checkbox1", 240, 88, 13, 17)
;^^\<<<<<<<<<<<<<<<
GUICtrlSetState(-1, $GUI_CHECKED)
$colum[1] = GUICtrlCreateCheckbox("Checkbox2", 336, 88, 13, 17)
;^^\<<<<<<<<<<<<<<<
GUICtrlSetState(-1, $GUI_CHECKED)
$colum[2] = GUICtrlCreateCheckbox("Checkbox3", 440, 88, 13, 17)
;^^\<<<<<<<<<<<<<<<
GUICtrlSetState(-1, $GUI_CHECKED)
Dim $row[4] ;<<<<<<<<<<<<<<<
$row[0] = GUICtrlCreateCheckbox("Checkbox4", 160, 120, 13, 17)
;^^\<<<<<<<<<<<<<<<
GUICtrlSetState(-1, $GUI_CHECKED)
$row[1] = GUICtrlCreateCheckbox("Checkbox5", 160, 160, 13, 17)
;^^\<<<<<<<<<<<<<<<
GUICtrlSetState(-1, $GUI_CHECKED)
$row[2] = GUICtrlCreateCheckbox("Checkbox6", 160, 200, 13, 17)
;^^\<<<<<<<<<<<<<<<
GUICtrlSetState(-1, $GUI_CHECKED)
$row[3] = GUICtrlCreateCheckbox("Checkbox7", 160, 234, 13, 17)
;^^\<<<<<<<<<<<<<<<
GUICtrlSetState(-1, $GUI_CHECKED)
$Label1 = GUICtrlCreateLabel("Random Pick:", 320, 344, 71, 17)
$Label2 = GUICtrlCreateLabel("0", 416, 320, 68, 64)
GUICtrlSetFont(-1, 33, 400, 0, "MS Sans Serif")
$pickone = GUICtrlCreateButton("Pick One", 120, 336, 75, 25, 0)
$Input1 = GUICtrlCreateInput("1,2,3,4,5,6,7,8,33,44,90,91,92,93,94,95,96", 224, 24, 289, 21)
$Label3 = GUICtrlCreateLabel("Must be on of these numbers:", 64, 24, 144, 17)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $pickone
            $sizeofarray = 0
            $result = ""
            For $x = 0 To 2 ;<<<<<<<<< Because of Array, easy walk-through smile.gif
                If _IsChecked($colum[$x]) Then
                    For $y = 0 To 3 ;<<<<<<<<<
                        If _IsChecked($row[$y]) Then
                            $result &= $RowCol[$x][$y] & "|" ;<<<<<<<<<
                        
                        EndIf
                    Next
                EndIf
                
            Next
            MsgBox( 0, "",$result)
            
            Dim $checkedoptions = StringSplit($result, "|", 1)
            Local $sizeofcheckedoptions = UBound($checkedoptions, 1) - 1
            Dim $alsorequired = _ArrayCreate(GUICtrlRead($Input1))
            Local $result = ""
            For $x = 0 To $sizeofcheckedoptions
                Local $search = _ArraySearch($alsorequired, $checkedoptions[$x])
                MsgBox( 0, "", $search)
                If $search NOT Then
                    $result &= $checkedoptions[$x] & "|" ;<<<<<<<<<
                EndIf
            Next

            MsgBox(0, '', $result)
            
            ;now how pick a number out that is:
            ; 1. part of any colum that has been ticked
            ; 2. part of any row that has been ticked
            ; 3. is part of the required numbers
    EndSwitch
WEnd

Func _IsChecked($control)
    Return BitAND(GUICtrlRead($control), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>_IsChecked

Cheers,

Adrian

Link to comment
Share on other sites

Wohoo finally got it, I guess I just needed some sleep =)

Thanks

Case $pickone
            $size = 0
            Dim $matrixmatch[1]
            For $x = 0 To 2 ;<<<<<<<<< Because of Array, easy walk-through smile.gif
                If _IsChecked($colum[$x]) Then
                    For $y = 0 To 3 ;<<<<<<<<<
                        If _IsChecked($row[$y]) Then
                            If $size = 0 Then
                                $matrixmatch[0] = $RowCol[$x][$y]
                                $size = $size + 1
                            Else
                                _ArrayAdd($matrixmatch, $RowCol[$x][$y])

                            EndIf
                        EndIf
                    Next
                EndIf
            Next
            ;_ArrayDisplay( $matrixmatch)
            Dim $alsorequired = StringSplit(GUICtrlRead($Input1), ",", 1)
            _ArrayDelete($alsorequired, 0)
            ;_ArrayDisplay( $alsorequired)
            $sizeofalsorequired = UBound($alsorequired, 1)
            $searchresult = 0
            $size = 0
            Dim $finalarray[1]
            For $i = 0 To $sizeofalsorequired -1
                $searchresult = _ArraySearch($matrixmatch, $alsorequired[$i])
                If @error Then
                    ;do nothing
                ElseIf $size = 0 Then
                    $finalarray[0] = $matrixmatch[$searchresult]
                    $size = $size + 1
                Else
                    _ArrayAdd($finalarray, $matrixmatch[$searchresult])
                EndIf
            Next
            ;_ArrayDisplay( $finalarray)
            ;now pick a random number
            $sizeoffinalarray = UBound( $finalarray, 1)
            $randomnote = $finalarray[Random( 0, $sizeoffinalarray)]
            MsgBox( 0, "", "random note is: " & $randomnote)
    EndSwitch
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...