0

I am looking for the following if it exists in code (any language is ok, but preferably c#):

a,b,limit,key : integers

0< a, b, < limit (fits in less than int32)

b = forth(a, limit, key)
a = back (b, limit, key)

I need something that will look sort-of random so (=) and (xor) are not good enough.

Now I looked into block ciphers and pseudorandom generators and like but it's always about encryption and security and speed and uses the whole size of the integer. I don't care about any of those. All I need is a bijection on the domain where average of forth(a+epsilon) >> b+epsilon

KF2
  • 9,887
  • 8
  • 44
  • 77
CxDoo
  • 121
  • 1
  • 9
  • Can you clarify "average of forth(a+epsilon) >> b+epsilon"? What is the >> operator in this case? – Alex Apr 04 '13 at 11:22
  • basically for a, a+1, a+2, ... i don't wan't f(a), f(a+1), ... to be sequential or even linear. For example 1,2,3 -> 3005, 3008, 3011 is NOT ok. 1, 2, 3 -> 3001, 7243, 8555 is OK. – CxDoo Apr 04 '13 at 11:40

1 Answers1

0

Anything wrong with something simple like the following?

int Forth(int a, int limit, int key)
{
    return (limit - a) ^ key;
}

int Back(int b, int limit, int key)
{
    return limit - (b ^ key);
}

EDIT: Adapted from here (does not use a key though - is this necessary?)

int Forth(int a)
{
    return ((0x00FF & a)<<8) + ((0xFF00 & a)>>8);
}

int Back(int b)
{
    return ((0x00FF & b)<<8) + ((0xFF00 & b)>>8);
}

N.B. Back() and Forth() are the same function above...

Community
  • 1
  • 1
Alex
  • 7,639
  • 3
  • 45
  • 58
  • forth(1,1000000,432654)=645169 and forth(2,1000000,432654)=645168. Too close. – CxDoo Apr 04 '13 at 10:07
  • Is this what you mean by "sort-of random"? How close is too close? – Alex Apr 04 '13 at 11:21
  • I answered above. Maybe I wasn't too clear, I don't wan't a linear function, ideally i would like something like pseudorandom generator of known length (limit) that goes through all values from 1..limit-1. – CxDoo Apr 04 '13 at 11:42
  • See update - if you need something more specific than this then I guess you will have to figure out an algorithm to suit your exact needs – Alex Apr 04 '13 at 11:50
  • The update doesn't satisfy the condition [1..limit]->[1..limit]. I tried all this but thanks for help anyway. – CxDoo Apr 04 '13 at 12:45