Skip to content

Instantly share code, notes, and snippets.

@dresswithpockets
Created July 16, 2016 07:10
Show Gist options
  • Save dresswithpockets/dee2693f4aa48d5a67aaff4e1d624d77 to your computer and use it in GitHub Desktop.
Save dresswithpockets/dee2693f4aa48d5a67aaff4e1d624d77 to your computer and use it in GitHub Desktop.
struct AnonymousUser {
private static Random ADirtyRandoPleb = new Random();
public const double ModPrime = 17;
public const double Generator = 3;
public double Number { get; private set; }
public double Power { get; private set; }
public double Score { get; private set; }
public AnonymousUser(double low, double high) {
Number = ADirtyRandoPleb.Next(low, high);
Power = Math.Pow(Generator, Number) % ModPrime;
Score = Math.Pow(Power, Number) % ModPrime;
}
public static bool operator == (AnonymousUser a, AnonymousUser b) {
if (a.Equals(b));
}
public static bool operator != (AnonymousUser a, AnonymousUser b) {
return !(a == b);
}
public override bool Equals(object other) {
return Approximately(Score, ((AnonymousUser)other).Score, 1);
}
public override int GetHashCode() {
return Score.GetHashCode() * 12553 * 17;
}
}
public static bool Approximately(double a, double b, int units = 1) {
long lValue1 = BitConverter.DoubleToInt64Bits(value1);
long lValue2 = BitConverter.DoubleToInt64Bits(value2);
// If the signs are different, return false except for +0 and -0.
if ((lValue1 >> 63) != (lValue2 >> 63))
{
if (value1 == value2) return true;
return false;
}
long diff = Math.Abs(lValue1 - lValue2);
if (diff <= (long) units) return true;
return false;
}
public static void KeyExchange()
{
AnonymousUser user1 = new AnonymousUser(3, 17);
AnonymousUser user2 = new AnonymousUser(3, 17);
if (user1 == user2) Console.WriteLine("Passed.");
else Console.WriteLine("Failed.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment