SerialCode - Module / SerialNumber - Module

Share your advanced PureBasic knowledge/code with the community.
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

SerialCode - Module / SerialNumber - Module

Post by StarBootics »

Hello everyone,

Two different Modules inspired from this post : http://www.purebasic.fr/english/viewtop ... 17&t=34979

Best regards
StarBootics

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : SerialCode - Module
; File Name : SerialCode - Module.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 19-02-2015
; Last Update : 27-01-2016
; PureBasic code : V5.41 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; This code was originally created by Kale to generate a serial code.
;
; Source : http://www.purebasic.fr/english/viewtopic.php?f=17&t=34979 
;
; I deserve credit only to convert the original code into a Module.
;
; This code is free to be use where ever you like but you use it at 
; your own risk.
;
; The author can in no way be held responsible for data loss, damage
; or other annoying situations that may occur.
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule SerialCode
  
  Declare.s Make(GroupSize.l, GroupCount.l)
  
EndDeclareModule

Module SerialCode
  
  Procedure.s Make(GroupSize.l, GroupCount.l)
    
    For GroupID = 1 To GroupCount
      
      For Size = 1 To GroupSize
        Output.s + Chr(Random(90, 65))
      Next
      
      If GroupID < GroupCount
        Output + "-"
      EndIf
      
    Next
    
    ProcedureReturn Output
  EndProcedure
  
EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  Macro AddElementEx(ListName, Element)
    
    AddElement(ListName)
    ListName = Element
    
  EndMacro
  
  NewList Serials.s()
  
  For x = 1 To 100
    AddElementEx(Serials(), SerialCode::Make(5, 5))
  Next
  
  SortList(Serials(), #PB_Sort_Ascending)
  
  ForEach Serials()
    Debug Serials()
  Next
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : SerialNumber - Module
; File Name : SerialNumber - Module.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 22-02-2015
; Last Update : 27-01-2016
; PureBasic code : V5.41 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; This code was originally created by milan1612 to generate a serial
; number.
;
; Source : http://www.purebasic.fr/english/viewtopic.php?f=17&t=34979 
;
; I deserve credit only to convert the original code into a Module.
;
; This code is free to be use where ever you like but you use it at 
; your own risk.
;
; The author can in no way be held responsible for data loss, damage
; or other annoying situations that may occur.
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule SerialNumber
  
  Declare.s Make(P_CrossSum.l)
  Declare SanityCheck(P_SerialNumber.s, P_CrossSum.l)
  
EndDeclareModule

Module SerialNumber
  
  Procedure.s Make(P_CrossSum.l)
    
    While Cross <> P_CrossSum
      
      Cross = 0
      
      For i = 0 To 4
        
        For j = 0 To 4
          Digit = Random(9)
          Temp.s + Str(Digit)
          Cross + Digit
        Next
        
        Select i
            
          Case 0
            Field00.s = Temp
            
          Case 1
            Field01.s = Temp
            
          Case 2
            Field02.s = Temp
            
          Case 3
            Field03.s = Temp
            
          Case 4
            Field04.s = Temp
            
        EndSelect

        Temp = ""

      Next
      
    Wend
    
    ProcedureReturn Field00 + "-" + Field01 + "-" + Field02 + "-" + Field03 + "-" + Field04
  EndProcedure
  
  Procedure SanityCheck(P_SerialNumber.s, P_CrossSum.l)
    
    SerialLenMax = Len(P_SerialNumber)
    
    For CharID = 1 To SerialLenMax
      
      Select Asc(Mid(P_SerialNumber, CharID, 1))
      
      	Case '-'
      
      	Case '0' To '9'
      	  Cross + Val(Mid(P_SerialNumber, CharID, 1))
      	  
      EndSelect
      
    Next
    
    If Cross = P_CrossSum
      ProcedureReturn 1
    EndIf
    
  EndProcedure

EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  ;You can freely choose your desired "magic" cross sum - but it should be between ~100 to ~200!
  Define t, sn.s
  
  For t = 1 To 50
    
    sn = SerialNumber::Make(150)
    Debug sn
    Debug SerialNumber::SanityCheck(sn, 150)
    Debug ""
    
  Next
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
The Stone Age did not end due to a shortage of stones !