Serial Numbers
#1
Well hello again, I'm here with another bit of code, I was pondering the idea of a way to add serial codes and code checks to my programs, and this is what i came up with to generate a serial number>
Code: (Select All)
Function genCode$
    Dim As Single one, two, three: one = 8: two = 7: three = 4
    Dim code As String
    '1
    Do
        For i = 1 To 5
            x$ = x$ + _Trim$(Str$(Int(Rnd * 10)))
        Next
        If Val(x$) Mod one = 0 Then
            Exit Do
        Else
            x$ = ""
        End If
    Loop
    code = x$
    x$ = ""
    '2
    Do
        For i = 1 To 7
            x$ = x$ + _Trim$(Str$(Int(Rnd * 10)))
        Next
        If Val(x$) Mod two = 0 Then
            Exit Do
        Else
            x$ = ""
        End If
    Loop
    code = code + "-" + x$
    x$ = ""
    '3
    Do
        For i = 1 To 5
            x$ = x$ + _Trim$(Str$(Int(Rnd * 10)))
        Next
        If Val(x$) Mod three = 0 Then
            Exit Do
        Else
            x$ = ""
        End If
    Loop

and this is what checks to see if the code is valid, and then returns a 1 for valid and a 0 for invalid>
Code: (Select All)
Function checkcode (d As String)
    Dim As Single one, two, three: one = 8: two = 7: three = 4
    Dim As String a, b, c

    If InStr(d, "-") = 0 Then
        a = Mid$(d, 1, 5)
        b = Mid$(d, 6, 7)
        c = Mid$(d, 13, 5)
        Print a, b, c
    End If

    a = Left$(d, InStr(d, "-") - 1)
    b = Mid$(d, InStr(d, "-") + 1, 7)
    c = Right$(d, 5)

    If Val(a) Mod one = 0 And Val(b) Mod two = 0 And Val(c) Mod three = 0 Then
        If d = "" Then checkcode = 0: Exit Function
        checkcode = 1
    Else
        checkcode = 0
    End If
End Function


Attached Files
.bm   serial.bm (Size: 1.46 KB / Downloads: 21)
Reply


Messages In This Thread
Serial Numbers - by AtomicSlaughter - 03-11-2023, 11:40 PM
RE: Serial Numbers - by mnrvovrfc - 03-13-2023, 05:45 PM



Users browsing this thread: 4 Guest(s)