Help needed on an Algorithm

  • Thread starter Thread starter Ian Baker
  • Start date Start date
I

Ian Baker

I am trying to create an algorithm in Access 2003 vba from variables of x
and y that will result in a 12 digit number without any recurring pattern
when the value of x increases.

x can be between 38517 and 45822
y can be between 1252 and 6100

so to encode x and y:
if y=2450 and x=38517 then z will equal for example 123456789876
if y=2450 and x=38838 then z will equal for example 98765432123
if y=2450 and x=39122 then z will equal for example 552288664479
etc

Then I need to be able to decode the z value to ascertain what the x and y
values were.
for example:
if z=123456789876 then y=2450 and x=38517
if z=98765432123then y=2450 and x=38838
if z=552288664479then y=2450 and x=39122
etc

If anyone can help I would greatly appreciate it as I have spent days on
this using functions like XOR etc and just when I thought I was getting
close I couldn't either decode to ascertain what the x and y values were or
when encoding a pattern always emerged.

Thanks in advance
 
Hi Ian Baker,

Thanks for using MSDN Newsgroup!

From your descriptions I understood you would like to find a algorithm to
resolve your issue, however, I am not very sure for how could you make z
from x and y. Do you make them by means of some rules or randomly? If there
are some rules, would you please show me how to caculate thme?

Unforutnately, nor could I fingure out how could you decode z successfully
to y and x. would you please so kind as to show me it rules?

Let's keep an eye on other community members, who might has better idea :)

Thank you for your patience and cooperation. If you have any questions or
concerns, don't hesitate to let me know. We are here to be of assistance!


Sincerely yours,

Mingqing Cheng
Microsoft Developer Community Support
 
Hi Mingqing
Firstly thankyou for responding to my post.

Re what you have replied is well "that is the question!". To make it a bit
clearer here is some code that I created but does not work for all numbers
in the range of x or y and it ends up only changing the last 5 numbers or
so:
'<start code>
Dim x As Double 'date number
Dim y As Double 'number derived from Institution Name
Dim z As Double 'total using x & y
Dim n As Double 'constant
Dim w1 As Double 'constant
Dim w2 As Double 'constant
Dim u1 As Double 'total for U = Z xor W1
Dim u2 As Double 'total for U = Z xor W2

x = 38517 'date number will increase by 365 +/- 30 each year
y = 3446 'different institutions have different numbers between 1252 & 6100
n = 74283391 'random selected 8 digit constant
w1 = 274877 'left 6 digits of a random number between 2^38 and 2^39
w2 = 906944 'right 6 digits of the random number for w1

z = x + (y * n)

u1 = Left(z, 6) Xor w1 'Use the left 6 digits of z
u2 = Right(z, 6) Xor w2 'Use the right 6 digits of z

Debug.Print u1 & u2 'print the 12 digit answer in the debug window

'Now go backwards from the u1 & u2 12 digit number to get x & y which will
be a
'seperate function - note whilst y will be a variable it is known when the
algorithm is run
Dim z1 As Double
Dim z2 As Double

z1 = u1 Xor w1
z2 = u2 Xor w2

z = z1 & z2

x = z - (y * n)

Debug.Print x 'print the x value in the debug window
Debug.Print y 'print the y value in the debug window
'<end code>
 
Hi Ian,

I will be OOF for two days and I will keep you updated ASAP when I back.

Thank you for your patience and cooperation again. If you have any
questions or concerns, don't hesitate to let me know. We are here to be of
assistance!


Sincerely yours,

Mingqing Cheng
Microsoft Developer Community Support
 
Hello Ian,

MingQing discussed that with me before he is OOF. I reviewed the post and found that the question is not related to Access. More precisely, it
is a question on how to design an algorithm.

If that, comp.programming group should be a better place to ask this question. If you met problem when implementing it in VBA in Access.,
this group may be more helpful.

By the way, I also reviewed your post. It seems that n, w1, w2 are all random numbers. If that, how to regenerate x, y from z. Do you mean you
will store all the n, w1, w2 somewhere?

Thanks.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Ian,

I can't think of any useful purpose this will serve, but here are a
couple of simple functions. An improvement might be to use a 3-digit
random number instead of a 4-digit one, and use the digit saved for a
check digit for detecting errors.

Function Scramble(X As Long, Y As Long) As String

Dim strX As String
Dim strY As String
Dim Z As Long
Dim strZ As String
Dim strS As String
Dim j As Long

Z = CLng(Rnd(CDbl(Now())) * 10000)
X = (X + Z - 38517) Mod 10000
Y = (Y + Z - 1252) Mod 10000
strX = Format(X, "0000")
strY = Format(Y, "0000")
strZ = Format(Z, "0000")

For j = 1 To 4
strS = strS & Mid(strZ, j, 1) & Mid(strY, j, 1) & Mid(strX, j, 1)
Next

Scramble = strS
End Function

Sub Unscramble(S As String, ByRef X As Long, ByRef Y As Long)

Dim strX As String, strY As String, strZ As String
Dim Z As Long
Dim j As Long

If Len(S) <> 12 Then
MsgBox "Someone has blundered"
End If

For j = 1 To 12 Step 3
strZ = strZ & Mid(S, j, 1)
strY = strY & Mid(S, j + 1, 1)
strX = strX & Mid(S, j + 2, 1)
Next

Z = CLng(strZ)
Y = (CLng(strY) + 1252 - Z)
X = (CLng(strX) + 38517 - Z)
End Sub

Sub TestScramble(X As Long, Y As Long)
Dim lngX As Long, lngY As Long
Dim strS As String

strS = Scramble(X, Y)
Unscramble strS, lngX, lngY
Debug.Print strS, lngX, lngY
End Sub
 
Hi John
Thankyou for your reply. What you have suggested is brilliant and gives me
something to work from to end up with what it will be used for. If I may be
so bold I will send you an email with how it is to be used. Hopefully you
will receive it.
 
You'll need to decode my reversed s p a m trap, but I'll be interested
to see what the need is!
 
Back
Top