views:

157

answers:

3

So I'm creating a Activation class for a VB6 project and I've run into a brain fart. I've designed how I want to generate the Serial Number for this particular product in a following way.

XXXX-XXXX-XXXX-XXXX

Each group of numbers would be representative of data that I can read if I'm aware of the matching document that allows me to understand the codes with the group of digits. So for instance the first group may represent the month that the product was sold to a customer. But I can't have all the serial numbers in January all start with the same four digits so there's some internal math that needs to be done to calculate this value. What I've landed on is this:

A B C D = digits in the first group of the serial number (A + B) - (C + D) = #

Now # would relate to a table of Hex values that would then represent the month the product was sold. Something like...

1 - January

2 - February

3 - March

....

B - November

C - December

My question lies here - if I know I need the total to equal B(11) then how exactly can I code backwards to generate (A + B) - (C + D) = B(11)?? It's a pretty simple equation, I know - but something I've just ran into and can't seem to get started in the right direction. I'm not asking for a full work-up of code but just a push. If you have a full solution available and want to share I'm always open to learning a bit more.

I am coding in VB6 but VB.NET, C#, C++ solutions could work as well since I can just port those over relatively easily. The community help is always greatly appreciated!

+2  A: 

There's no single solution (you have one equation with four variables). You have to pick some random numbers. Here's one that works (in Python, but you get the point):

from random import randint

X = 11 # the one you're looking for

A_plus_B = randint(X, 30)
A = randint(max(A_plus_B - 15, 0), min(A_plus_B, 15))
B = A_plus_B - A

C_plus_D = A_plus_B - X
C = randint(max(C_plus_D - 15, 0), min(C_plus_D, 15))
D = C_plus_D - C

I assume you allow hexadecimal digits; if you just want 0 to 9, replace 15 by 9 and 30 by 18.

balpha
We must have been updating at the same time. Thank for the response! It helped solidify that this approach will work for me.
Jeff
A: 

OK - pen and paper is always the solution... so here goes...

Attempting to find what values should be for (A + B) - (C + D) to equal a certain number called X. First I know that I want HEX values so that limits me to 0-F or 0-15. From there I need a better starting place so I'll generate a random number that will represent the total of (A + B), we'll call this Y, but not be lower than value X. Then subtract from that number Y value of X to determine that value that will represent (C + D), which we'll call Z. Use similar logic to break down Y and Z into two numbers each that can represent (A + B) = Y and (C + D) = Z. After it's all said and done I should have a good randomization of creating 4 numbers that when plugged into my equation will return a suitable result.

Just had to get past the brain fart.

Jeff
A: 

This may seem a little hackish, and it may not take you where you're trying to go. However it should produce a wider range of values for your key strings:

Option Explicit

Private Function MonthString(ByVal MonthNum As Integer) As String
    'MonthNum: January=1, ... December=12.  Altered to base 0
    'value for use internally.
    Dim lngdigits As Long

    MonthNum = MonthNum - 1
    lngdigits = (Rnd() * &H10000) - MonthNum
    MonthString = Right$("000" & Hex$(lngdigits + (MonthNum - lngdigits Mod 12)), 4)
End Function

Private Function MonthRecov(ByVal MonthString As String) As Integer
    'Value returned is base 1, i.e. 1=January.
    MonthRecov = CInt(CLng("&H" & MonthString) Mod 12) + 1
End Function

Private Sub Form_Load()
    Dim intMonth As Integer
    Dim strMonth As String
    Dim intMonthRecov As Integer
    Dim J As Integer

    Randomize
    For intMonth = 1 To 12
        For J = 1 To 2
            strMonth = MonthString(intMonth)
            intMonthRecov = MonthRecov(strMonth)
            Debug.Print intMonth, strMonth, intMonthRecov, Hex$(intMonthRecov)
        Next
    Next
End Sub
Bob Riemersma