Jump to content

cant get correct output...


LurchMan
 Share

Recommended Posts

Hey guys -

The other day I got bored and wanted to see if i could write a random key generator. Well now that i actually found a use for it I added another array and a For..Next loop to check the new generated key against whats already in the file. The output is all wacky and they are not unique keys each time it outputs it. I'm not really sure where I went wrong. I attached the output file to show what it does.

Each time it prints a new key it prints it more times than the previous time it printed one.

Code:

#include <File.au3>

Opt("TrayIconDebug", 1) 

Global $aString = StringSplit ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", "")
Global $aKey[20]
Global $sFile = @Scriptdir & "\keys.txt"
Global $Cnt = 1
Global $aCheckKey 

While $Cnt < 11
;~  Generate 20 Char Random Key
    For $n = 0 to 19
        $i = Random(1, 36)
        $aKey[$n] = $aString[$i]
    Next

;~  Read File to array
_FileReadToArray ($sFile, $aCheckKey)

;~  Set new generated key into single variable
$sKey = $aKey[0] & $aKey[1] & $aKey[2] & $aKey[3] & $aKey[4] & $aKey[5] & $aKey[6] & _
                $aKey[7] & $aKey[8] & $aKey[9] & $aKey[10] & $aKey[11] & $aKey[12] & $aKey[13] & $aKey[14] & _
                $aKey[15] & $aKey[16] & $aKey[17] & $aKey[18] & $aKey[19]

;~   check new key against keys already in the file
    For $k = 1 To UBound($aCheckKey) - 1
        If $aCheckKey[$k] = $sKey Then 
    Else
        FileWriteLine($sFile, $sKey)
    EndIf   
Next
$Cnt = $Cnt + 1
WEnd

Any help would be appreciated greatly...Thank you.

keys.txt

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Link to comment
Share on other sites

  • Moderators

LurchMan,

A few changes needed, but this seems to work well. It adds unique keys to the file and rejects matches (you can test it by setting $sKey to an existing value where shown):

#include <File.au3>

Opt("TrayIconDebug", 1)

Global $aString = StringSplit("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", "")
Global $sFile = @ScriptDir & "\keys.txt"
Global $aCheckKey[1], $fMatch

;~  Create file if it does not exist
If Not FileExists($sFile) Then FileWrite($sFile, "")

For $Cnt = 1 To 10
;~  Generate 20 Char Random Key
    $sKey = ""
    For $n = 0 To 19
        $sKey &= $aString[Random(1, 36, 1)]
    Next

;~  If you want to check the match logic, add an existing key and uncomment the next line
;~  $sKey = ########################

;~  Read File to array
    _FileReadToArray($sFile, $aCheckKey)
;~  If no file then create blank array
    If Not IsArray($aCheckKey) Then Dim $aCheckKey[1]

;~  Check new key against keys already in the file
    $fMatch = False
    For $k = 0 To UBound($aCheckKey) - 1
        If $aCheckKey[$k] = $sKey Then $fMatch = True
    Next
    If Not $fMatch Then FileWriteLine($sFile, $sKey)

Next

Major changes:

- key generated by adding each element to a string in a loop rather than by using array elements and then adding them together (looks neater)

- 10 x loop using For...Next rather than While...WEnd and a count (let AutoIt do the work)

- use a boolean flag to mark a key match (lets you scan the whole fiel before deciding)

- added errorchecking for no file - create an empty file and a dummy array so your match loop has something to work on (be nice to the first user)

Plus a few other little things. ;-)

Ask if anything is unclear.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

thanks for the help! but there's 2 lines that I dont understand how they work. i would appreciate it if you could explain it to me.

This line:

$sKey &= $aString[Random(1, 36, 1)]

^^ - Dont understand this part

And this line:

If Not $fMatch Then FileWriteLine($sFile, $sKey)

^^^^^^^ dont understand how this works

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Link to comment
Share on other sites

  • Moderators

LurchMan,

1. $sKey &= $aString[Random(1, 36, 1)]. The earlier line $sKey = "" creates an empty string. The loop goes around 20 times and picks a new character each time. The &= operator adds this character to the empty string. Look in the Help file under "Language Reference - Operators".

2. If Not $fMatch Then FileWriteLine($sFile, $sKey). $fMatch is a boolean variable - that means it is set to either True or False. When you use an If statement, you are basically asking a Boolean question - for example If x = 1 is True if x = 1 and False in every other case. So all we are doing here is asking the If statement if $fMatch is True or False. But becasue we want to do something if $fMatch is False, we have to invert the question by using the Not operator - going back to our example, Not x = 1 is False if x = 1 and True in all other cases.

Boolean logic can seem a bit daunting at first, but it is very simple really. Play with a few scripts using If, Not, True and False and you will soon get the hang of it.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

thank you that actually makes sense now... :)

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

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...