Sign in to follow this  
Followers 0
lordpier

random with unique result

16 posts in this topic

Hello,

I know the subject has already been treated in the forum but my request is a bit different.

I use a function to determine a random number between 1 and 500. Every time I run the macro, I want a random number that have never been taken before (unless you reset it of course). A kind of lotto.

Here is a code i started. The GUI displays the number and saves it to a text file. And here I am seeking for your help : What I do not know is how to have a function that gives a random number that is not in the text file (or it can be an .ini file I guess)

Thank you for your help

$results1 = Random(1, 500, 1)

$file = "number.txt"

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)
Opt("GUIOnEventMode", 1)

Global $ExitID

_Main()

Func _Main()
   
       
    GUICreate("random", 600, 160)

    GUICtrlCreateLabel("randomizator :", 10, 10)
    GUICtrlCreateLabel($results1, 120, 10)
   
    GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit")

FileWrite("number.txt", $results1 & @CRLF)


    GUISetState()

    While 1
        Sleep(1000)
    WEnd
EndFunc

Func OnExit()
    If @GUI_CtrlId = $ExitID Then
        Exit
    Else
        Exit
    EndIf

    Exit
EndFunc

Share this post


Link to post
Share on other sites



FileReadLine would read the line from a file (do this for every line in a loop, look at the given example), and then you just compare it to $results1 for each line, if it is the same, don't write.

If $results1 = $LineFromFile Then

;Nothing

Else

;Stuff to do with the new number.

Endif

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

you can try it like this, its fast enough

$numbers = 500
Global $r,$str
While 1
    $r = String(";"&Random(1,$numbers,1)&";")
    If Not StringInStr($str,$r,1) Then
        $str = $str&$r
        $bla = StringSplit($str,";;",1)
        MsgBox(0,$bla[0]&"   "&$str,$r)
    EndIf
    If $bla[0] = $numbers Then
        MsgBox(0,"THE","END")
        Exit
    EndIf
WEnd

edited: added exit when end is reached

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

thanks for fast answers !

I didn't succeed to add your codes to mine. I re-specify my request :

- The random number I get shoud never appear again, even if I close the gui & re-open after, that's why it should be record in a text file.

- If the random number is already in the text file, it should give another one (loop until find a number never appeared) & record it in the text file (if never appeared).

I hope it is understandable...

thanx for help

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

noone is gona write it completely for you, you gota put some effort in it, you have all you need, only thing you need is to write it on one place and to load it to $str on start of the code

Ini*

File*

from help file

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Hi,

like this:

#include <GUIConstantsEx.au3>
#include <file.au3>
#include <array.au3>

Opt('MustDeclareVars', 1)
Opt("GUIOnEventMode", 1)

Global $ExitID, $arnumber [1], $results1, $numberfile = "number.txt", $file
_FileReadToArray ($numberfile, $arnumber) 
$file = FileOpen ($numberfile, 1)
_Main()

Func _Main()
   
    GUICreate("random", 600, 160)

    GUICtrlCreateLabel("randomizator :", 10, 10)
    local $rlabel = GUICtrlCreateLabel("", 120, 10, 500, 150)
   
    GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit")

    GUISetState()

    While 1
        $results1 = Random(1, 500, 1)
        If _Arraysearch ($arnumber, $results1) < 1 Then
            $arnumber [UBound ($arnumber) - 1] = $results1
            Redim $arnumber [UBound ($arnumber) + 1]
            FileWrite($file, $results1 & @CRLF)
            If GUICtrlRead ($rlabel) <> "" Then
                GUICtrlSetData ($rlabel, GUICtrlRead ($rlabel) & ", " & $results1)
            Else
                GUICtrlSetData ($rlabel, $results1)
            EndIf
            local $btnid = MsgBox (1, "New number", "Generate new number?")
            If $btnid = 2 Then
                FileClose ($file)
                Exit
            EndIf
        EndIf
        Sleep(20)
    WEnd
EndFunc

Func OnExit()
    FileClose ($file)
    If @GUI_CtrlId = $ExitID Then
        Exit
    Else
        Exit
    EndIf

    Exit
EndFunc

;-))

Stefan

@bogQ: Just have had nothing to do.......

Edited by 99ojo

Share this post


Link to post
Share on other sites

Hi,

thanx so much,

I wouldn't succeed it by myself... But it works perfectly !

I will try to put an error message when all numbers has been edited, and a reset option. Hope I will succeed without asking you again...

Thanx to all

Share this post


Link to post
Share on other sites

@bogQ: Just have had nothing to do.......

ok then, at least to post my way :/

#include <GUIConstantsEx.au3>
Global $str,$r,$c,$numbers = 10,$bla 

If Not FileExists("myfile.ini") Then
    IniWrite("myfile.ini", "section2", "key", "")
Else
    $str = IniRead("myfile.ini", "section2", "key", "default" )
EndIf
$bla = StringSplit($str,";;",1)
reset()
$G_1 = GUICreate("random", 150, 130)
$B_1 = GUICtrlCreateButton("Generate", 10, 50, 100)
$B_2 = GUICtrlCreateButton("Reset", 10, 80, 100)
$L_1 = GUICtrlCreateLabel("Randomizator :", 10, 10)
$L_2 = GUICtrlCreateLabel("NONE", 120, 10)
GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $B_2
            $6 = MsgBox(4,"RESET?","???")
                If $6 = 6 Then
                    IniWrite("myfile.ini", "section2", "key", "")
                    $str = IniRead("myfile.ini", "section2", "key", "default" )
                EndIf
        Case $msg = $B_1
            GUICtrlSetData($L_2, randomnum())
            $c = 0
            If $bla[0] = $numbers Then
                reset()
            EndIf
    EndSelect
WEnd

Func randomnum()
    While $c <> 1
        $r = String(";"&Random(1,$numbers,1)&";")
        If Not StringInStr($str,$r,1) Then
            $str = $str&$r
            $bla = StringSplit($str,";;",1)
            $c = 1
            IniWrite("myfile.ini", "section2", "key", $str)
            Return $r
        EndIf
    WEnd
EndFunc

Func reset()
    If $bla[0] >= $numbers Then
        $6 = MsgBox(4,"THE END","RESET???")
        If $6 = 6 Then
            IniWrite("myfile.ini", "section2", "key", "")
            $str = IniRead("myfile.ini", "section2", "key", "default" )
        Else
            Exit
        EndIf
    EndIf
EndFunc

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

Hi,

thanx so much,

I wouldn't succeed it by myself... But it works perfectly !

I will try to put an error message when all numbers has been edited, and a reset option. Hope I will succeed without asking you again...

Thanx to all

Hi,

no problem. Just keep in mind, that program execution will get very slow, if your 'number pool runs empty'. At least it could take 'hours' for generating a number, which isn't in the number.txt.

I would generate numbers from 1000 - 59999. Then divide them with 1000 and do a Round and then compare with the Array.

$results1 = Round (Random(1000, 59999, 1)/1000, -1)

;-))

Stefan

Share this post


Link to post
Share on other sites

wow, too fast for me ! ;)

no problem. Just keep in mind, that program execution will get very slow, if your 'number pool runs empty'. At least it could take 'hours' for generating a number, which isn't in the number.txt.

I would generate numbers from 1000 - 59999. Then divide them with 1000 and do a Round and then compare with the Array.

$results1 = Round (Random(1000, 59999, 1)/1000, -1)

thanx for advice, I will check it.

ok then, at least to post my way :/

oh ok, now I understand what you meant with your code in your first post...

Thanx for challenging with Stephan ! It made my problem solved ! (But i realize i have alot to learn !)

thanx again men !

Share this post


Link to post
Share on other sites

hello,

I have a little problem. I do something wrong & it is not working properly.What I try to do, it's open one random file from folders (.rar protected with password). But I dont succeed to integrate the random code what bogQ did.

here is the code I was using :

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)
Opt("GUIOnEventMode", 1)

Global $ExitID

_Main()

Func _Main()
    Local $AllID
    
    
    GUICreate("survey correction", 600, 160)

    GUICtrlCreateLabel("A survey will be proposed for you", 10, 10)
    $AllID = GUICtrlCreateButton("All", 380, 0, 110, 120)
    GUICtrlSetOnEvent($AllID, "OnAll")
    $ExitID = GUICtrlCreateButton("Exit", 500, 0, 100, 160)
    GUICtrlSetOnEvent($ExitID, "OnExit")
    
    GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit")

    GUISetState()  ; display the GUI

    While 1
        Sleep(1000)
    WEnd
EndFunc   ;==>_Main


;--------------- Functions ---------------
Func OnAll()
    Local $results, $results1, $results2, $results3, $results4, $results5, $i, 
    
Sleep(2000)
$results = Random(1, 5, 1)
$results1 = Random(1, 400, 1)
$results2 = Random(1, 400, 1)
$results3 = Random(1, 400, 1)
$results4 = Random(1, 400, 1)
$results5 = Random(1, 200, 1)

If $results = 1 Then
ShellExecute("C:\survey\class1.rar")
Sleep(800)
Send("pass{ENTER}")
Sleep(2000)
Send("{DOWN}{ENTER}")
Sleep(1500)
    $i=0
    While 1
    if $i= $results1 then Exitloop
    Send("{down}")
    $i=$i+1
Wend
Send("{ENTER}")
Sleep(8000)
ProcessClose("WinRAR.exe")
EndIf

If $results = 2 Then
ShellExecute("C:\survey\class2.rar")
Sleep(800)
Send("pass{ENTER}")
Sleep(2000)
Send("{DOWN}{ENTER}")
Sleep(1500)
    $i=0
    While 1
    if $i= $results2 then Exitloop
    Send("{down}")
    $i=$i+1
Wend
Send("{ENTER}")
Sleep(8000)
ProcessClose("WinRAR.exe")
EndIf

If $results = 3 Then
ShellExecute("C:\survey\class3.rar")
Sleep(800)
Send("pass{ENTER}")
Sleep(2000)
Send("{DOWN}{ENTER}")
sleep(1500)
    $i=0
    While 1
    if $i= $results3 then Exitloop
    Send("{down}")
    $i=$i+1
Wend
Send("{ENTER}")
Sleep(8000)
ProcessClose("WinRAR.exe")
EndIf

If $results = 4 Then
ShellExecute("C:\survey\class4.rar")
Sleep(800)
Send("pass{ENTER}")
Sleep(2000)
    $i=0
    While 1
    if $i= $results4 then Exitloop
    Send("{down}")
    $i=$i+1
Wend
Send("{ENTER}")
Sleep(8000)
ProcessClose("WinRAR.exe")
EndIf

If $results = 5 Then
ShellExecute("C:\survey\class5.rar")
Sleep(800)
Send("pass{ENTER}")
Sleep(2000)
    $i=0
    While 1
    if $i= $results5 then Exitloop
    Send("{down}")
    $i=$i+1
Wend
Send("{ENTER}")
Sleep(8000)
ProcessClose("WinRAR.exe")
EndIf

EndFunc   ;==>OnAll


Func OnExit()
    If @GUI_CtrlId = $ExitID Then
        Exit
    Else
        Exit
    EndIf

    Exit
EndFunc   ;==>OnExit

So I tried to insert my code inside BogQ code. But it's not working...

#include <GUIConstantsEx.au3>
Global $str,$r,$c,$numbers = 400,$bla, $i


If Not FileExists("myfile.ini") Then
    IniWrite("myfile.ini", "section2", "key", "")
Else
    $str = IniRead("myfile.ini", "section2", "key", "default" )
EndIf
$bla = StringSplit($str,";;",1)
reset()
$G_1 = GUICreate("random", 250, 150)
$B_1 = GUICtrlCreateButton("Generate", 10, 50, 100)
$B_2 = GUICtrlCreateButton("Reset", 10, 80, 100)
$L_1 = GUICtrlCreateLabel("Randomizator :", 10, 10)
$L_2 = GUICtrlCreateLabel("NONE", 120, 10)
GUISetState()





While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $B_2
            $6 = MsgBox(4,"RESET?","???")
                If $6 = 6 Then
                    IniWrite("myfile.ini", "section2", "key", "")
                    $str = IniRead("myfile.ini", "section2", "key", "default" )
                EndIf
        Case $msg = $B_1
            GUICtrlSetData($L_2, randomnum())
            $c = 0
            If $bla[0] = $numbers Then
                reset()
            EndIf
    EndSelect
WEnd

Func randomnum()
    While $c <> 1
        $r = String(";"&Random(1,$numbers,1)&";")
        If Not StringInStr($str,$r,1) Then
            $str = $str&$r
            $bla = StringSplit($str,";;",1)
            $c = 1
            ShellExecute("C:\survey\class1.rar")
            Sleep(800)
            Send("pass{ENTER}")
            Sleep(2000)
            Send("{DOWN}{ENTER}")
            Sleep(1500)
            Send("{DOWN}{DOWN}{DOWN}{ENTER}")
            sleep(500)
            Send("{ENTER}")
            $i=0
                While 1
                if $i= $r then Exitloop
                Send("{down}")
                $i=$i+1
                Wend
                Send("{ENTER}")
                Sleep(8000)
                ProcessClose("WinRAR.exe")
            IniWrite("myfile.ini", "section2", "key", $str)
        EndIf
            Return $r
    WEnd
EndFunc


Func reset()
    If $bla[0] >= $numbers Then
        $6 = MsgBox(4,"THE END","RESET???")
        If $6 = 6 Then
            IniWrite("myfile.ini", "section2", "key", "")
            $str = IniRead("myfile.ini", "section2", "key", "default" )
        Else
            Exit
        EndIf
    EndIf
EndFunc

I have in reality 5 folders, but i make a try just with the first. It's working fine to open the folder but the loop to reach the file is not working. This part exatly :

While 1
                if $i= $r then Exitloop
                Send("{down}")
                $i=$i+1
                Wend
                Send("{ENTER}")
                Sleep(8000)

Where I make a mistake ? And where to put the function to choose between my 5 folders ($results = Random(1, 5, 1)) ?

thanks

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

so only thing you need is to randomm open one of rar files?

r all rar files named class1 class2 class3 class4 class5 class6 class7 .....?

Im little lost here with your current problem, i don't understand it ^^. Can you try to one more time explaine what whas your next step with the script that your haveing problems with?

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

so only thing you need is to randomm open one of rar files?

r all rar files named class1 class2 class3 class4 class5?

Im little lost here with your current problem, i don't understand it ^^.

oh ok ! I try to explain it again a bit more clearly :

My first code to open randomly a file is working, I just want to add your code (myfile.ini) to avoid somebody get the same file

I have 5 rar files named respectively class1 class2 class3 class4 class5.

When I click on generate, I wish the program open randomly one of the .rar (5 posbility so) & open randomly one file from this rar (400 possibility).

But a file mustn't appear twice, that's the reason of the myfile.ini.

mmmmmm, i hope it's understandable....;)

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

oh ok ! I try to explain it again a bit more clearly :

My first code to open randomly a file is working, I just want to add your code (myfile.ini) to avoid somebody get the same file

I have 5 rar files named respectively class1 class2 class3 class4 class5.

When I click on generate, I wish the program open randomly one of the .rar (5 posbility so) & open randomly one file from this rar (400 possibility).

But a file mustn't appear twice, that's the reason of the myfile.ini.

mmmmmm, i hope it's understandable....;)

Global $str,$r,$c,$numbers = 400,$bla 
#include <GUIConstantsEx.au3>

If Not FileExists("myfile.ini") Then
    IniWrite("myfile.ini", "section2", "key", "")
Else
    $str = IniRead("myfile.ini", "section2", "key", "default" )
EndIf
$bla = StringSplit($str,";;",1)
reset()
$G_1 = GUICreate("random", 150, 130)
$B_1 = GUICtrlCreateButton("Generate", 10, 50, 100)
$B_2 = GUICtrlCreateButton("Reset", 10, 80, 100)
$L_1 = GUICtrlCreateLabel("Randomizator :", 10, 10)
$L_2 = GUICtrlCreateLabel("NONE", 120, 10)
GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $B_2
            $6 = MsgBox(4,"RESET?","???")
                If $6 = 6 Then
                    IniWrite("myfile.ini", "section2", "key", "")
                    $str = IniRead("myfile.ini", "section2", "key", "default" )
                EndIf
        Case $msg = $B_1
            $file = randomnum()
            GUICtrlSetData($L_2,$file)
            ShellExecute("class"&Random(1, 5, 1)&".rar")
            Sleep(800)
            Sleep(1500)
            For $i = 1 to StringReplace($file,";","")
                Send("{down}")
            Next
            Send("{ENTER}")
            Sleep(5000)
            Send("rara{ENTER}")
            Sleep(8000)
            ProcessClose("WinRAR.exe")
            IniWrite("myfile.ini", "section2", "key", $str)
            $c = 0
            If $bla[0] = $numbers Then
                reset()
            EndIf
    EndSelect
WEnd

Func randomnum()
    While $c <> 1
        $r = String(";"&Random(1,$numbers,1)&";")
        If Not StringInStr($str,$r,1) Then
            $str = $str&$r
            $bla = StringSplit($str,";;",1)
            $c = 1
            IniWrite("myfile.ini", "section2", "key", $str)
            Return $r
        EndIf
    WEnd
EndFunc

Func reset()
    If $bla[0] >= $numbers Then
        $6 = MsgBox(4,"THE END","RESET???")
        If $6 = 6 Then
            IniWrite("myfile.ini", "section2", "key", "")
            $str = IniRead("myfile.ini", "section2", "key", "default" )
        Else
            Exit
        EndIf
    EndIf
EndFunc

note that all my rar files r in script dir and every document in that rar file have password "rara"

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

thanx bogQ,

but it's not working properly :

- the exit button not working anymore

- the 5 rar folders don't have same number of files inside

- the .ini file is not carrying from which folder come from the opened file.

I will try to find out some solution...

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

thanx bogQ,

but it's not working properly :

- the exit button not working anymore

- the 5 rar folders don't have same number of files inside

- the .ini file is not carrying from which folder come from the opened file.

I will try to find out some solution...

- exit button?

- you newer toled that script need to remamber what rar what file is opened

- you can add more keyes to ini key1...key5 every key to remamber for what rar file is opened and to load that key

$k = string(Random(1, 5, 1))

$str = IniRead("myfile.ini", "section2", "key"&$k, "default" )

and after that

IniWrite("myfile.ini", "section2", "key"&$k, $str)

to remamber it

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0