Jump to content
PCI

Generate random numbers with validation check digits

Recommended Posts

Hi everyone 

I've read all yesterday forums , i think it's me , but i could not find a clue for this :

I need to generate large numbers and output into csv format or excel format

- Range between 3000000 and 9000000 

- Need 5000000 numbers

- Numbers should not be in series ( Mixed and Random )

- I would prefere to have a check digits at the end

Anyone could help please ?

 

Thank you

 

 

Share this post


Link to post
Share on other sites

PCI,

Random will produce the numbers for you (although 5000000 will take a little time) - and formatting the results as .csv should not prove difficult. But what do you mean by check digits?

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

 

Share this post


Link to post
Share on other sites

@Melba23 Thank you so much for your reply !

I need to produce set of 5 millions numbers the numbers , i need to make check digits at the end to validate the numbers with an algorithm like mod10 or luhrn .

I prefer to validate 8 digits

 

Please help

 

Thank you

Share this post


Link to post
Share on other sites

PCI,

Do you mean something like this:

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>


; Create file
$sFile = "Test.csv"

$hFile = FileOpen($sFile, $FO_OVERWRITE)

$sLine = ""


; Produce numbers (100 here as an
 example)
For $i = 1 To 100

    ; Create a random number
    $sNumber = String(3000000 + Random(0, 6000000, 1))

    ; Calculate a check digit
    $iCheckSum = 0
    For $j = 1 To 7
        $iCheckSum += StringMid($sNumber, $j, 1)
    Next
    $iCheckDigit = Mod($iChecksum, 10)

    ; Add the number and check digit to the line
    $sLine &= $sNumber & "-" & $iCheckDigit & ","

    ; Write every a line to the file every 5 numbers
    If Mod($i, 5) = 0 Then
        $sLine = StringTrimRight($sLine, 1)
        FileWrite($hFile, $sLine & @CRLF)
        $sLine = ""
    EndIf

Next


; Close the file
FileClose($hFile)


; And here is the result
$sData = FileRead($sFile)
MsgBox($MB_SYSTEMMODAL, "Random", $sData)

Just out of interest, why do you need so many random numbers?

M23

Edit:

And here is the Luhn version (now I know what it is!):

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

$sFile = "Test.csv"

$hFile = FileOpen($sFile, $FO_OVERWRITE)

$sLine = ""

For $i = 1 To 100

    $sNumber = String(3000000 + Random(0, 6000000, 1))
    $iCheckSum = 0
    For $j = 1 To 7
        $iCheckSum += StringMid($sNumber, $j, 1)
    Next
    $iCheckSum *= 9
    $sCheckDigit = StringRight(String($iCheckSum), 1)

    $sLine &= $sNumber & $sCheckDigit & ","

    If Mod($i, 5) = 0 Then
        $sLine = StringTrimRight($sLine, 1)
        FileWrite($hFile, $sLine & @CRLF)
        $sLine = ""
    EndIf

Next

FileClose($hFile)

$sData = FileRead($sFile)
MsgBox($MB_SYSTEMMODAL, "Random", $sData)

 

Edited by Melba23

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

 

Share this post


Link to post
Share on other sites


Thank you so much @Melba23 you ROCK !
I'm really suprized how clean and easy to read
I'm trying to distibute a unique code to random friends to access a web page
However i could not generate the results with these requirements
   - Fixed length if possible 
   - Range from 1000000(1M) to 40000000(40M)
   - Mod10 check
   - Could i add carriage return to the output so they are only 1 set per ligne ?
   
   Thank you for making me learn , this i will make a gui with prompt to enter low range and hi range and select mod10 with check then click on output
      
      Great Forum / Great work / Genius Autoit 

Share this post


Link to post
Share on other sites

PCI,

Of course the numbers are not Mod 10 - you did not ask for them to be so.  The code produces a Mod 10 checksum and adds it to the end of the random value - as this checksum will vary between 0 and 9, the final digit will also vary between those values.

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

 

Share this post


Link to post
Share on other sites

Thank you 

You are very helpful .

I will try and add mod10 calculation

I think it will be very hard for a newbie like me. :-)

Any suggestions where to start please ?

Regards

 

Share this post


Link to post
Share on other sites

Melba something looks wrong with your Luhn algorithm code. Here's my version. BTW your _ArrayDisplay range feature was very useful testing my code. :)

PCI - The numbers you want to generate seem rather a lot. The method I use below generates Luhn numbers of 8 digits. Using _ArrayShuffle to create larger numbers is not a simple procedure. The reason is that arrays have a maximum limit of approx 16000000 elements. Only some clever work-around will allow you to exceed this range limit: assuming the numbers must be unique, as random as possible (within the range) and appear in any order. I have suspicions that you don't actually need all these numbers, but hopefully the code below will help. If there's something you don't understand then ask.

#include <Array.au3>

; Maximum valid 8 digits Luhn Number = 99999997

; Generate numbers from 1000000 to 9999999
Local $iMin = 1000000
Local $iMax = 9999999
Local $iBound = $iMax - $iMin +1

Local $aArray[$iBound]
For $i = 0 To $iBound -1
    $aArray[$i] = $iMin + $i
Next

_ArrayShuffle($aArray) ; Shuffle the numbers

Local $iIterations = 1000 ; Suppose we only need 1000 random 8 digit Luhn numbers (for quick demonstration purposes)

ReDim $aArray[$iIterations] ; Delete most of the array

; Aquire the last digit: needed to convert each 7 digit number to a valid 8 digit Luhn number
For $i = 0 To $iIterations -1
    $aArray[$i] &= GenerateLastLuhnDigit($aArray[$i])
Next

_ArrayDisplay($aArray)

Func GenerateLastLuhnDigit($vNumber)
    Local $iSum = 0, $sCurrDigit
    For $i = StringLen($vNumber) To 1 Step -2 ; Every second number from the right will be doubled.
        $sCurrDigit = StringMid($vNumber, $i, 1) ; Read each digit to be doubled starting from the right.
        $sCurrDigit *= 2 ; Multiply by 2.
        If $sCurrDigit > 9 Then $sCurrDigit = StringLeft($sCurrDigit, 1) + StringRight($sCurrDigit, 1) ; Make sure $sCurrDigit is not greater than 9.
        $iSum += $sCurrDigit + StringMid($vNumber, $i -1, 1) ; Also add the odd step value (not multiplied by 2)
    Next
    $iSum *= 9 ; Multiply the result by 9.
    Return Mod($iSum, 10) ; This digit can be appended to $vNumber to create a valid Luhn number.
EndFunc

 

Edited by czardas
spelling

Share this post


Link to post
Share on other sites

Hi czardas 

You guys are Genius !!!

I'm testing as we speak , i'm trying to make a GUI to Input low range and High Range in this GUI also any possibility to have it output to a text file ?

Thank you so much

Share this post


Link to post
Share on other sites

No problem, I guess you might just want to use the function GenerateLastLuhnDigit() to generate the final digit. It should work on any number size, but very large numbers must be passed as strings. The rest of my code is really intended to give you some ideas to play around with. Good luck. :)

Edited by czardas

Share this post


Link to post
Share on other sites

Cool worked just fine :)

Very happy guys Thank you

However , i have limitations to the output due to the maximum row 65524 , any thing i could do to output this as text file ?

Thank you for the help 

God bless you

 

 

Share this post


Link to post
Share on other sites

You can also change the iterations to a maximum of 9000000 (any higher and the code will give you an error), but I don't know how long it will take to generate all the numbers. Probably not too long - you can try it if you want. However you will not be able to display all of the array - only parts of it. Write to file as you yourself suggested.

Edited by czardas

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

  • Similar Content

    • By v0id
      I am starting out using AutoIt. Here is a simple form with username and password. I want to check if information entered is valid once user clicks a button.
      My problem now is that it only validates once. E.g.: if I type 5 character username, it will complain it is not 7 character (good). But once I correct that mistake and press the button again it will still say the same thing.
      Do I need to have a loop?
       
      #include <GUIConstantsEx.au3> #include <EditConstants.au3> #include <MsgBoxConstants.au3> Opt("GUIOnEventMode", 1) $main = GUICreate("Test Tool", 600, 600) $hyourlabel = GUICtrlCreateLabel("YOUR CREDENTIALS", 30, 10, 256) GUICtrlSetFont($hyourlabel, Default, 600) Local $adminfrejalabel = GUICtrlCreateLabel("Username:", 8, 38, 64, 17) Global $adminfrejaid = GUICtrlCreateInput("", 80, 38, 110, 17) Local $adminpasswordlabel = GUICtrlCreateLabel("Password:", 8, 62, 64, 17) Global $adminpassword = GUICtrlCreateInput("", 80, 62, 110, 17, BitOR($ES_PASSWORD, $ES_AUTOHSCROLL)) $userButton_Check = GUICtrlCreateButton("VALIDATE", 32, 480, 85, 25) GUICtrlSetOnEvent($userButton_Check, "startvalidation") GUISetOnEvent($GUI_EVENT_CLOSE, "ExitGUI") GUISetState(@SW_SHOW) While 1 Sleep(10) WEnd Func startvalidation() ;CHECK VALIDATIONS $adminfrejaid = GUICtrlRead($adminfrejaid) $adminpassword = GUICtrlRead($adminpassword) If StringLen($adminfrejaid) <> '7' Then MsgBox($MB_SYSTEMMODAL, "User ID", "Please enter exactly 7 characters.") ;Exit EndIf If StringLen($adminpassword) < '5' Then MsgBox($MB_SYSTEMMODAL, "Your Password", "Please enter a valid password.") ;Exit EndIf EndFunc Func ExitGui () Exit ; Exit the program EndFunc  
    • By matwachich
      HI everybody! It's been a (very) long time since I posted something useful!
      Here I come with a new functions that I needed in a project that needs many different forms to insert different data into a database. Instead of creating a different Koda form for each data type, I decided to create this function, inspired by this one (from the excellent IUP GUI library).
      To create an input dialog, you first must create a JSON definition of this dialog. Simple example of a login form:
      { title: "Dialog Title", controls: [ {type:"label", value:"Please login"}, {type:"input", id:"user", label:"User name"}, {type:"input", id:"pass", label:"Password", style:$ES_PASSWORD}, {type:"check", id:"remember", label:"Remember me", value:true} ] } Then pass it to the advInputBox function:
      #include "advInputBox.au3" Dim $oRet = advInputBox($sJSON) If @error Then MsgBox(64, "Example1", "Dialog canceled") Else MsgBox(64, "Example1", "Return: " & Json_Encode($oRet, 128)) ; you can access individual values by ids: ; Json_ObjGet($oRet, "username") ; Json_ObjGet($oRet, "password") ; Json_ObjGet($oRet, "remember") EndIf And you will get an InputBox like this one:

      The code is on GutHub
      Enjoy!
       
    • By JustinZandee
      I need a random string generator which creates 15 letters/numbers.
      How can I make that?
    • By nacerbaaziz
      Hi dears
      How are you?
      I have a question, to you please.
      How do I create an edit box for numbers only and does not accept letters? using autoit
      greetings to all
      I hope you help ME
      Thanks to all in advance
    • By Anas
      Hi,
      I've this script (removed unrelated parts) that generate a set of random chars for a password. It gets launched from a network share by Task Scheduler at a specific time for all PC's, then it relaunches itself from the local drive.
       
      Global $LocalToolsDir = @ProgramFilesDir & '\Tools' Start() Func Start() If Not StringInStr(@ScriptFullPath, $LocalToolsDir, 2) Then Exit RunLocaly() Sleep(Random(1, 1800, 1) * 1000) $NewPass = GeneratePass() EndFunc Func GeneratePass() Local $Pass For $i = 1 to 8 $R = Random(0,1.5) If $R > 1 Then $Chr = Random(0,9,1) ElseIf $R < 0.5 Then $Chr = Chr(Random(Asc("A"), Asc("Z"), 1)) Else $Chr = Chr(Random(Asc("a"), Asc("z"), 1)) Endif $Pass &= $Chr Next Return $Pass EndFunc Func RunLocaly() Local $Run = 1, $LocalApp = $LocalToolsDir & '\' & @ScriptName If Not FileExists($LocalApp) Then $Run = FileCopy(@ScriptFullPath, $LocalToolsDir, 1 + 8) If $Run Then Run($LocalApp, $LocalToolsDir) EndFunc  
      The problem is that many PC's ends up with the same password(s).
      e.g. 10 PC's have (abc123) as a Password, and another 10 have this (def456)
      Any idea why?
×
×
  • Create New...