Jump to content

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

 

 

Link to comment
Share on other sites

  • Moderators

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

 

Link to comment
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

Link to comment
Share on other sites

  • Moderators

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

 

Link to comment
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 

Link to comment
Share on other sites

  • Moderators

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

 

Link to comment
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

 

Link to comment
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
Link to comment
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

Link to comment
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
Link to comment
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

 

 

Link to comment
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
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

×
×
  • Create New...