0

I have a simple function that handles overflows with modulus operator

   Private Function RandomizeBlock(seed As Integer, ByVal block() As Byte) As Byte()
        Dim Generator As System.Random = New System.Random(seed)
        Dim newblock(255) As Byte
        Dim i As Integer = 0
        For i = 0 To block.Length - 1
            newblock(i) = (block(i) + Generator.Next(0, 256)) Mod 256
        Next

        Return newblock
    End Function

How do I undo the randomization done to a block?

I know mod works like this:

253,254,255,0,1,2,3,4 overwraps on 0.

Do can I find the inverse of reverse here?

rndValue = Generator.Next(0, 256)
reverse_1 = ((256 - rndValue) + block(i)) Mod 256
reverse_2 = ((256 + rndValue) - block(i)) Mod 256
SSpoke
  • 5,656
  • 10
  • 72
  • 124
  • It’s unclear what you’re trying to achieve: you seem to know that `Mod` is an irreversible function in general. You have `x + b Mod 256 = a`, and you seem to want to retrieve `x` (mod 256). But this will only work if you know `a` *and* `b`. At the moment you only know `a`, because `b` is for all intents and purposes not inferable. This cannot work. – Konrad Rudolph Aug 03 '16 at 20:54
  • nothing is lost with the mod operator it's just held in what I call muscle memory even though its overflowed back to a lower number it could still be considered a big number imaginary and subtracted from it.. no? I know the RNG value that was added and I know the new value which was modded. – SSpoke Aug 03 '16 at 21:09
  • Mod just returns the remainder after division, so when you set remainder = x Mod 256, you get a number representing the amount left over after dividing x by however many 256's fit. To 'undo' the Mod function, you'd need to know how many 256's fit in addition to the remainder. – Beth Aug 03 '16 at 21:19
  • There will only be 1 256 ever done. – SSpoke Aug 03 '16 at 21:21
  • If the purpose of the method is "just" to shuffle the given array ; you should use a simpler shuffling algorithmfor example the [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm) – Sehnsucht Aug 03 '16 at 21:28
  • @Sehnsucht looking for a way to transform the array to get more random results and still be able to reverse it back when needed. – SSpoke Aug 04 '16 at 21:01

1 Answers1

1

If you know the random value, then reconstructing the original value is very simple.

You just have to keep in mind that working modulo p, you don't have actual numbers but remainder classes. The first p natural numbers are usually used as the representatives of these classes. Luckily, subtraction and addition are perfectly compatible with remainder classes.

The Mod implementation of VB converts any positive number to the representative of its remainder class. However, it can not do it on negative numbers. You have to do it on your own.

Long story short, this is the code:

Dim reverse As Integer = block(i) - rndValue;
If reverse < 0 Then reverse = reverse + 256 'Convert to representative of remainder class
Nico Schertler
  • 32,049
  • 4
  • 39
  • 70