Jump to content

Recommended Posts

Posted
Hello friends.

How can I prevent the same number from being repetitive in this program. Thank you in advance for your help.

image.png.f6ef0ed0422e27b3bc26715c7a391496.png

#include <Array.au3>
#include <GuiConstants.au3>

Dim $Num[6]
Global $_Chck = False
Global $Gui = GUICreate("", 610, 170, -1,-1, $WS_POPUP+$WS_BORDER)
GUISetBkColor(0x3366FF)

Local $Genr = GUICtrlCreateButton("Generate Number", 10, 10, 300, 35)
GUICtrlSetFont(-1, 20, 800,  0, "Arial Narrow")

Local $Exit = GUICtrlCreateButton("CLOSE", 500, 10, 100, 35)
GUICtrlSetFont(-1, 20, 800,  0, "Arial Narrow")

Global $I[6]

For $N = 0 To 5
    $I[$N] = GUICtrlCreateInput("", 10 + 100 * ($N), 60, 90, 100, $ES_READONLY+$ES_CENTER)
    GUICtrlSetFont(-1, 70, 800,  0, "Arial Narrow")
Next
GUISetState()

While 1
    $Msg = GUIGetMsg()
    Select
        Case $Msg = -3 Or $Msg = $Exit
            Exit
        Case $Msg = $Genr
            If $_Chck = False Then
                $_Chck = True
            Else
                $_Chck = False
            EndIf
            If $_Chck = True Then
                GUICtrlSetData($Genr, 'STOP')
                AdlibRegister('_RandomNum', 10)
            EndIf
            If $_Chck = False Then
                GUICtrlSetData($Genr, 'Generate Number')
                _ArraySort($Num)
                For $N = 0 To 5
                    GUICtrlSetData($I[$N], $Num[$N])
                Next
                AdlibUnRegister('_RandomNum')
            EndIf
    EndSelect
WEnd

Func _RandomNum()
    For $N = 0 To 5
        $Num[$N] = Random(1, 90, 1)
        GUICtrlSetData($I[$N], $Num[$N])
    Next
EndFunc

:hyper:

Posted

Here a simple way :

Func _RandomNum()
  For $N = 0 To 5
    $Num[$N] = Random(1, 90, 1)
    For $C = 0 To 5
      If $C = $N Then ContinueLoop
      If $Num[$C] = $Num[$N] Then
        $N -= 1
        ;ConsoleWrite ("Found duplicate" & @CRLF)
        ContinueLoop 2
      EndIf
    Next
    GUICtrlSetData($I[$N], $Num[$N])
  Next
EndFunc   ;==>_RandomNum

 

Posted (edited)

Another simple way:

#include <Array.au3>


_ArrayDisplay(lotto_numbers(), "Lotto Numbers")

Func lotto_numbers()
    Local $aAvailableNumbers[0], $aSelectedNumbers[0]
    Local $iRandomIndex

    ;Load available numbers
    For $i = 1 To 90
        _ArrayAdd($aAvailableNumbers, $i)
    Next

    ;Select 6 random, non-repeating, numbers from available numbers
    For $i = 1 To 6
        $iRandomIndex = Random(0, UBound($aAvailableNumbers) - 1, 1)    ;Random available number index
        _ArrayAdd($aSelectedNumbers, $aAvailableNumbers[$iRandomIndex]) ;Add available number to selected numbers
        _ArrayDelete($aAvailableNumbers, $iRandomIndex)                 ;Remove selected number from available numbers
    Next

    Return $aSelectedNumbers
EndFunc

 

Edited by TheXman
Posted

Another skin for this cat:

Local $N = 99
Local $a[$N]
For $i = 0 To $N - 1
    $a[$i] = $i + 1
Next

For $i = 1 To 8
    _ArrayShuffle($a)
    ; display 6 random numbers without dups
    ConsoleWrite(_ArrayToString($a, " ", 0, 5, " ") & @LF)
    Sleep(1000)
Next

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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
  • Recently Browsing   0 members

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