A class that adapts a random byte generator to generate random numbers in a variety of statistical distributions.

The method descriptions in this class assume the underlying random byte generator generates uniformly distributed numbers that are independent of each other.

Thread safety: The methods in this class are safe for concurrent use by multiple threads, as long as the underlying random byte generator is as well.

Exemplo n.º 1
0
 public static string RandomTextString(RandomGenerator rand) {
   int length = rand.UniformInt(0x2000);
   var sb = new StringBuilder();
   for (var i = 0; i < length; ++i) {
     int x = rand.UniformInt(100);
     if (x < 95) {
       // ASCII
       sb.Append((char)(0x20 + rand.UniformInt(0x60)));
     } else if (x < 98) {
       // Supplementary character
       x = rand.UniformInt(0x400) + 0xd800;
       sb.Append((char)x);
       x = rand.UniformInt(0x400) + 0xdc00;
       sb.Append((char)x);
     } else {
       // BMP character
       x = 0x20 + rand.UniformInt(0xffe0);
       if (x >= 0xd800 && x < 0xe000) {
         // surrogate code unit, generate ASCII instead
         x = 0x20 + rand.UniformInt(0x60);
       }
       sb.Append((char)x);
     }
   }
   return sb.ToString();
 }
Exemplo n.º 2
0
 public static String RandomDecimalString(RandomGenerator r)
 {
     int count = r.UniformInt(40) + 1;
       var sb = new StringBuilder();
       if (r.UniformInt(2) == 0) {
     sb.Append('-');
       }
       for (var i = 0; i < count; ++i) {
     if (i == 0 && count > 1) {
       sb.Append((char)('1' + r.UniformInt(9)));
     } else {
       sb.Append((char)('0' + r.UniformInt(10)));
     }
       }
       if (r.UniformInt(2) == 0) {
     sb.Append('.');
     count = r.UniformInt(30) + 1;
     for (var i = 0; i < count; ++i) {
       sb.Append((char)('0' + r.UniformInt(10)));
     }
       }
       if (r.UniformInt(2) == 0) {
     sb.Append('E');
      count = (r.UniformInt(100) < 10) ? r.UniformInt(5000) :
       r.UniformInt(20);
     if (count != 0) {
       sb.Append(r.UniformInt(2) == 0 ? '+' : '-');
     }
     sb.Append(TestCommon.IntToString(count));
       }
       return sb.ToString();
 }
Exemplo n.º 3
0
 public static byte[] RandomByteStringShort(RandomGenerator rand) {
   int x = rand.UniformInt(50);
   var bytes = new byte[x];
   for (var i = 0; i < x; ++i) {
     bytes[i] = unchecked((byte)rand.UniformInt(256));
   }
   return bytes;
 }
Exemplo n.º 4
0
 public static ERational RandomERational(RandomGenerator rand) {
   EInteger bigintA = RandomEInteger(rand);
   EInteger bigintB = RandomEInteger(rand);
   if (bigintB.IsZero) {
     bigintB = EInteger.One;
   }
   return ERational.Create(bigintA, bigintB);
 }
Exemplo n.º 5
0
 public static string RandomLatinOne(RandomGenerator rnd)
 {
     int length = rnd.UniformInt(50) + 1;
       var sb = new StringBuilder();
       for (var i = 0; i < length; ++i) {
     var c = (char)rnd.UniformInt(256);
     sb.Append(c);
       }
       return sb.ToString();
 }
Exemplo n.º 6
0
 public static CBORObject RandomCBORArray(RandomGenerator rand, int depth)
 {
     int x = rand.UniformInt(100);
       int count = (x < 80) ? 2 : ((x < 93) ? 1 : ((x < 98) ? 0 : 10));
       CBORObject cborRet = CBORObject.NewArray();
       for (var i = 0; i < count; ++i) {
     cborRet.Add(RandomCBORObject(rand, depth + 1));
       }
       return cborRet;
 }
Exemplo n.º 7
0
 public static long RandomInt64(RandomGenerator rand) {
   long r = rand.UniformInt(0x10000);
   r |= ((long)rand.UniformInt(0x10000)) << 16;
   if (rand.UniformInt(2) == 0) {
     r |= ((long)rand.UniformInt(0x10000)) << 32;
     if (rand.UniformInt(2) == 0) {
       r |= ((long)rand.UniformInt(0x10000)) << 48;
     }
   }
   return r;
 }
Exemplo n.º 8
0
 public static CBORObject RandomCBORMap(RandomGenerator rand, int depth)
 {
     int x = rand.UniformInt(100);
       int count = (x < 80) ? 2 : ((x < 93) ? 1 : ((x < 98) ? 0 : 10));
       CBORObject cborRet = CBORObject.NewMap();
       for (var i = 0; i < count; ++i) {
     CBORObject key = RandomCBORObject(rand, depth + 1);
     CBORObject value = RandomCBORObject(rand, depth + 1);
     cborRet[key] = value;
       }
       return cborRet;
 }
Exemplo n.º 9
0
 public static String RandomBigIntString(RandomGenerator r)
 {
     int count = r.UniformInt(50) + 1;
       var sb = new StringBuilder();
       if (r.UniformInt(2) == 0) {
     sb.Append('-');
       }
       for (var i = 0; i < count; ++i) {
     if (i == 0) {
       sb.Append((char)('1' + r.UniformInt(9)));
     } else {
       sb.Append((char)('0' + r.UniformInt(10)));
     }
       }
       return sb.ToString();
 }
Exemplo n.º 10
0
 public static double RandomDouble(RandomGenerator rand, int exponent) {
   if (exponent == Int32.MaxValue) {
     exponent = rand.UniformInt(2047);
   }
   long r = rand.UniformInt(0x10000);
   r |= ((long)rand.UniformInt(0x10000)) << 16;
   if (rand.UniformInt(2) == 0) {
     r |= ((long)rand.UniformInt(0x10000)) << 32;
     if (rand.UniformInt(2) == 0) {
       r |= ((long)rand.UniformInt(0x10000)) << 48;
     }
   }
   r &= ~0x7ff0000000000000L;  // clear exponent
   r |= ((long)exponent) << 52;  // set exponent
   return BitConverter.ToDouble(BitConverter.GetBytes((long)r), 0);
 }
Exemplo n.º 11
0
 public void TestAdd() {
   try {
     EFloat.Zero.Add(null, EContext.Unlimited);
     Assert.Fail("Should have failed");
   } catch (ArgumentNullException) {
     new Object();
   } catch (Exception ex) {
     Assert.Fail(ex.ToString());
     throw new InvalidOperationException(String.Empty, ex);
   }
   var fr = new RandomGenerator();
   TestAddCloseExponent(fr, 0);
   TestAddCloseExponent(fr, 100);
   TestAddCloseExponent(fr, -100);
   TestAddCloseExponent(fr, Int32.MinValue);
   TestAddCloseExponent(fr, Int32.MaxValue);
 }
Exemplo n.º 12
0
 public void TestShiftLeft() {
   EInteger bigint = EInteger.One;
   bigint <<= 100;
   TestCommon.CompareTestEqualAndConsistent(bigint << 12, bigint >> -12);
   TestCommon.CompareTestEqualAndConsistent(bigint << -12, bigint >> 12);
   var r = new RandomGenerator();
   for (var i = 0; i < 1000; ++i) {
     EInteger bigintA = RandomBigInteger(r);
     EInteger bigintB = bigintA;
     for (int j = 0; j < 100; ++j) {
       EInteger ba = bigintA;
       ba <<= j;
       TestCommon.CompareTestEqualAndConsistent(bigintB, ba);
       int negj = -j;
       ba = bigintA;
       ba >>= negj;
       TestCommon.CompareTestEqualAndConsistent(bigintB, ba);
       bigintB *= (EInteger)2;
     }
   }
 }
Exemplo n.º 13
0
 public void TestPow() {
   var r = new RandomGenerator();
   for (var i = 0; i < 200; ++i) {
     int power = 1 + r.UniformInt(8);
     EInteger bigintA = RandomBigInteger(r);
     EInteger bigintB = bigintA;
     for (int j = 1; j < power; ++j) {
       bigintB *= bigintA;
     }
     DoTestPow(bigintA.ToString(), power, bigintB.ToString());
   }
 }
Exemplo n.º 14
0
    public void TestMultiplyDivideA() {
      var r = new RandomGenerator();
      for (var i = 1; i < 25; ++i) {
        for (var j = 1; j < i; ++j) {
          EInteger bigA, bigB;
          int highWord;
          bigA = AllOnesInteger(i);
          bigB = AllOnesInteger(j);
          TestMultiplyDivideOne(bigA, bigB);
          highWord = r.UniformInt(0x8000, 0x10000);
          bigA = EInteger.FromInt32(highWord).ShiftLeft(i * 16).Add(bigA);
          bigB = EInteger.FromInt32(highWord).ShiftLeft(j * 16).Add(bigB);
          TestMultiplyDivideOne(bigA, bigB);
          bigA = ZerosAndOnesInteger(i);
          bigB = ZerosAndOnesInteger(j);
          TestMultiplyDivideOne(bigA, bigB);
          highWord = r.UniformInt(0x8000, 0x10000);
          bigA = EInteger.FromInt32(highWord).ShiftLeft(i * 16).Add(bigA);
          bigB = EInteger.FromInt32(highWord).ShiftLeft(j * 16).Add(bigB);
          TestMultiplyDivideOne(bigA, bigB);
        }
      }
      EInteger startIntA = ZerosAndOnesInteger(100);
      EInteger startIntB = ZerosAndOnesInteger(50);
      for (var i = 1; i < 500; ++i) {
        EInteger bigA = FuzzInteger(startIntA, r.UniformInt(1, 100), r);
        EInteger bigB = FuzzInteger(startIntB, r.UniformInt(1, 50), r);
        TestMultiplyDivideOne(bigA, bigB);
      }
      {
        EInteger ei1 = EInteger.FromRadixString(
  "101000101010100000001000100000101000000000100000101000100010101000001010000000001010101000101010100010100",
  16);
        EInteger ei2 = EInteger.FromRadixString(
  "100000000000100010000000100000000010001000100010000010101010100000000000000000000",
  16);
        TestMultiplyDivideOne(ei1, ei2);
        ei1 = EInteger.FromString

  ("44461738044811866704570272160729755524383493147516085922742403681586307620758054502667856562873477505768158700319760453047044081412393321568753479912147358343844563186048273758088945022589574729044743021988362306225753942249201773678443992606696524197361479929661991788310321409367753462284203449631729626517511224343015354155975783754763572354740724506742793459644155837703671449155713000260325445046273385372701820583016334341594713806706345456633635125343104401883366671083569152");
                ei2 = EInteger.FromString

  ("6667912688606651657935168942074070387623462798286393292334546164025938697493268465740399785103348978411106010660409247384863031649363973174034406552719188394559243700794785023362300512913065060420313203793021880700852215978918600154969735168");
                TestMultiplyDivideOne (ei1, ei2);
      }
      for (var i = 0; i < 1000; ++i) {
        EInteger bigA, bigB;
        do {
          bigA = WordAlignedInteger(r);
          bigB = WordAlignedInteger(r);
        } while (bigA.CompareTo(bigB) <= 0);
        TestMultiplyDivideOne(bigA, bigB);
      }
      for (var i = 0; i < 10000; ++i) {
        EInteger bigA = RandomObjects.RandomEInteger(r);
        EInteger bigB = RandomObjects.RandomEInteger(r);
        TestMultiplyDivideOne(bigA, bigB);
      }
      TestMultiplyDivideOne(EInteger.FromInt32(-985), EInteger.FromInt32(0));
    }
Exemplo n.º 15
0
   private static EInteger FuzzInteger(
 EInteger ei,
 int fuzzes,
 RandomGenerator r) {
     byte[] bytes = ei.ToBytes(true);
     int bits = ei.GetUnsignedBitLength();
     for (var i = 0; i < fuzzes; ++i) {
       int bit = r.UniformInt(bits);
       bytes[bit / 8] ^= (byte)(1 << (bit & 0x07));
     }
     return EInteger.FromBytes(bytes, true);
   }
Exemplo n.º 16
0
    public void TestDivide() {
      int intA, intB;
      DoTestDivide

  ("39401404978667143593022554770633078187236345017741021112301638514137074723630749875836463116600753265992771999563776"
    ,
  "6277005955876855982708123588802319701019026907066160578560",
        "6277101735386680763835789423207589043669308487479442014208");
      DoTestDivide ("340277174703306882242637262502835978240",
        "79226953606891185567396986880", "4294967296");
      DoTestDivide (
  "44461738044811866704570272160729755524383493147516085922742403681586307620758054502667856562873477505768158700319760453047044081412393321568753479912147358343844563186048273758088945022589574729044743021988362306225753942249201773678443992606696524197361479929661991788310321409367753462284203449631729626517511224343015354155975783754763572354740724506742793459644155837703671449155713000260325445046273385372701820583016334341594713806706345456633635125343104401883366671083569152",
  "6667912688606651657935168942074070387623462798286393292334546164025938697493268465740399785103348978411106010660409247384863031649363973174034406552719188394559243700794785023362300512913065060420313203793021880700852215978918600154969735168",
  "6668014432879854274079851790721257797144739185760979705624542990230371779898108261760364709743735387156366994446448705720136517621612785459920009307944044809722559761949909348022458684413967432579072465854783948147327367860791365121685323776");
      DoTestDivide(
    "9999999999999999999999",
    "281474976710655",
    "35527136");
      DoTestDivide("2472320648", "2831812081", "0");
      DoTestDivide("-2472320648", "2831812081", "0");
      var fr = new RandomGenerator();
      for (var i = 0; i < 10000; ++i) {
        intA = fr.UniformInt(0x1000000);
        intB = fr.UniformInt(0x1000000);
        if (intB == 0) {
          continue;
        }
        int c = intA / intB;
        var bigintA = (EInteger)intA;
        var bigintB = (EInteger)intB;
        EInteger bigintC = bigintA / (EInteger)bigintB;
        Assert.AreEqual((int)bigintC, c);
      }
DoTestDivide ("4294901760", "281470681808895", "0");
            DoTestDivide ("281470681808895", "281470681808895", "1");
            DoTestDivide ("281195803901951", "281470681808895", "0");
        DoTestDivide ("281470681808895", "79226953606891185567396986880",
              "0");
   DoTestDivide ("1208907373151751269056511", "281470681808895",
              "4294967295");
            DoTestDivide ("1208907373151751269056511",
              "79226953606891185567396986880", "0");
            DoTestDivide ("79226953606891185567396986880",
              "79226953606891185567396986880", "1");
            DoTestDivide ("79226953606891185567396986880",
              "79226953606891185567396986880", "1");
            DoTestDivide ("79149582354435849300215791616",
              "281470681808895", "281200094609408");
            DoTestDivide ("79149582354435849304510693376",
              "79226953606891185567396986880", "0");
            DoTestDivide ("340277174703229510990181926235654782976",
              "79226953606891185567396986880", "4294967295");
            DoTestDivide ("340277174703229510990181926235654782976",
              "79226953606891185567396986880", "4294967295");
            DoTestDivide ("79226953606891185567396986880",
              "6277005955876855982708123588802319701019026907066160578560",
              "0");
            DoTestDivide ("22278626849872979772991819660510225504468991",
              "79226953606891185567396986880", "281200094609408");
            DoTestDivide
              ("6270875973713392427274690200693718464284551950581721071616",
  "79226953606891185567396986880",
              "79150790081217380608951451648");
            DoTestDivide
              ("6277005955876855982708123588802242329766571570798979383296",
  "6277005955876855982708123588802319701019026907066160578560",
  "0");
            DoTestDivide
              ("6277005955876855982708123588802242329766571570798979383296",
  "6277005955876855982708123588802319701019026907066160578560",
  "0");
            DoTestDivide
  ("26959535297282185466869868771998681536704617202858716036715199266816",
              "6277005955876855982708123588802319701019026907066160578560",
              "4294967295");
            DoTestDivide

  ("496829980752160275550680055858571148163286974448396184421327120687227627818219200249856"
    ,
  "6277005955876855982708123588802319701019026907066160578560",
  "79150790081217380608951451648");
            DoTestDivide

  ("2135954443842118711369801686589217620410698847025641089415087336821733096438436218376946913837056"
    ,
  "6277005955876855982708123588802319701019026907066160578560",
  "340282366920861091030327650447175712768");
    }
Exemplo n.º 17
0
 public void TestCanFitInInt() {
   var r = new RandomGenerator();
   for (var i = 0; i < 2000; ++i) {
     EInteger bigintA = RandomBigInteger(r);
     Assert.AreEqual(
           bigintA.CanFitInInt32(),
           bigintA.GetSignedBitLength() <= 31);
     Assert.AreEqual(
           bigintA.CanFitInInt64(),
           bigintA.GetSignedBitLength() <= 63);
   }
 }
Exemplo n.º 18
0
 public static EInteger RandomBigInteger(RandomGenerator r) {
   int selection = r.UniformInt(100);
   int count = r.UniformInt(60) + 1;
   if (selection < 40) {
     count = r.UniformInt(7) + 1;
   }
   if (selection < 50) {
     count = r.UniformInt(15) + 1;
   }
   var bytes = new byte[count];
   for (var i = 0; i < count; ++i) {
     bytes[i] = (byte)((int)r.UniformInt(256));
   }
   return BigFromBytes(bytes);
 }
Exemplo n.º 19
0
 public void TestToRadixString() {
   var fr = new RandomGenerator();
   try {
     EInteger.One.ToRadixString(-1);
     Assert.Fail("Should have failed");
   } catch (ArgumentException) {
     new Object();
   } catch (Exception ex) {
     Assert.Fail(ex.ToString());
     throw new InvalidOperationException(String.Empty, ex);
   }
   try {
     EInteger.One.ToRadixString(0);
     Assert.Fail("Should have failed");
   } catch (ArgumentException) {
     new Object();
   } catch (Exception ex) {
     Assert.Fail(ex.ToString());
     throw new InvalidOperationException(String.Empty, ex);
   }
   try {
     EInteger.One.ToRadixString(1);
     Assert.Fail("Should have failed");
   } catch (ArgumentException) {
     new Object();
   } catch (Exception ex) {
     Assert.Fail(ex.ToString());
     throw new InvalidOperationException(String.Empty, ex);
   }
   try {
     EInteger.One.ToRadixString(37);
     Assert.Fail("Should have failed");
   } catch (ArgumentException) {
     new Object();
   } catch (Exception ex) {
     Assert.Fail(ex.ToString());
     throw new InvalidOperationException(String.Empty, ex);
   }
   try {
     EInteger.One.ToRadixString(Int32.MinValue);
     Assert.Fail("Should have failed");
   } catch (ArgumentException) {
     new Object();
   } catch (Exception ex) {
     Assert.Fail(ex.ToString());
     throw new InvalidOperationException(String.Empty, ex);
   }
   try {
     EInteger.One.ToRadixString(Int32.MaxValue);
     Assert.Fail("Should have failed");
   } catch (ArgumentException) {
     new Object();
   } catch (Exception ex) {
     Assert.Fail(ex.ToString());
     throw new InvalidOperationException(String.Empty, ex);
   }
   for (int i = 2; i <= 36; ++i) {
     for (int j = 0; j < 100; ++j) {
       StringAndBigInt sabi = StringAndBigInt.Generate(fr, i);
       // Upper case result expected
       string expected = ToUpperCaseAscii(sabi.StringValue);
       var k = 0;
       // Expects result with no unnecessary leading zeros
       bool negative = sabi.BigIntValue.Sign < 0;
       if (expected[0] == '-') {
         ++k;
       }
       while (k < expected.Length - 1) {
         if (expected[k] == '0') {
           ++k;
         } else {
           break;
         }
       }
       expected = expected.Substring(k);
       if (negative) {
         expected = "-" + expected;
       }
       Assert.AreEqual(
         expected,
         sabi.BigIntValue.ToRadixString(i));
     }
   }
   var r = new RandomGenerator();
   for (var radix = 2; radix < 36; ++radix) {
     for (var i = 0; i < 80; ++i) {
       EInteger bigintA = RandomBigInteger(r);
       String s = bigintA.ToRadixString(radix);
       EInteger big2 = EInteger.FromRadixString(s, radix);
       Assert.AreEqual(big2.ToRadixString(radix), s);
     }
   }
 }
Exemplo n.º 20
0
 public void TestShiftRight() {
   EInteger bigint = EInteger.One;
   bigint <<= 80;
   TestCommon.CompareTestEqualAndConsistent(bigint << 12, bigint >> -12);
   TestCommon.CompareTestEqualAndConsistent(bigint << -12, bigint >> 12);
   var r = new RandomGenerator();
   EInteger minusone = EInteger.Zero;
   minusone -= EInteger.One;
   for (var i = 0; i < 1000; ++i) {
     int smallint = r.UniformInt(0x7fffffff);
     var bigintA = (EInteger)smallint;
     string str = bigintA.ToString();
     for (int j = 32; j < 80; ++j) {
       DoTestShiftRight2(str, j, EInteger.Zero);
       DoTestShiftRight2("-" + str, j, minusone);
     }
   }
   for (var i = 0; i < 1000; ++i) {
     EInteger bigintA = RandomBigInteger(r);
     bigintA = bigintA.Abs();
     EInteger bigintB = bigintA;
     for (int j = 0; j < 100; ++j) {
       EInteger ba = bigintA;
       ba >>= j;
       TestCommon.CompareTestEqualAndConsistent(bigintB, ba);
       int negj = -j;
       ba = bigintA;
       ba <<= negj;
       TestCommon.CompareTestEqualAndConsistent(bigintB, ba);
       bigintB /= (EInteger)2;
     }
   }
 }
Exemplo n.º 21
0
 public void TestSqrt() {
   var r = new RandomGenerator();
   for (var i = 0; i < 20; ++i) {
     EInteger bigintA = RandomBigInteger(r);
     if (bigintA.Sign < 0) {
       bigintA = -bigintA;
     }
     if (bigintA.Sign == 0) {
       bigintA = EInteger.One;
     }
     EInteger sqr = bigintA.Multiply(bigintA);
     EInteger sr = sqr.Sqrt();
     TestCommon.CompareTestEqual(bigintA, sr);
   }
   for (var i = 0; i < 10000; ++i) {
     EInteger bigintA = RandomBigInteger(r);
     if (bigintA.Sign < 0) {
       bigintA = -bigintA;
     }
     if (bigintA.Sign == 0) {
       bigintA = EInteger.One;
     }
     EInteger sr = bigintA.Sqrt();
     EInteger srsqr = sr * (EInteger)sr;
     sr += EInteger.One;
     EInteger sronesqr = sr * (EInteger)sr;
     if (srsqr.CompareTo(bigintA) > 0) {
       Assert.Fail(srsqr + " not " + bigintA +
         " or less (TestSqrt, sqrt=" + sr + ")");
     }
     if (sronesqr.CompareTo(bigintA) <= 0) {
       Assert.Fail(srsqr + " not greater than " + bigintA +
         " (TestSqrt, sqrt=" + sr + ")");
     }
   }
 }
Exemplo n.º 22
0
 public void TestGcd() {
   try {
     EInteger.Zero.Gcd(null);
     Assert.Fail("Should have failed");
   } catch (ArgumentNullException) {
     new Object();
   } catch (Exception ex) {
     Assert.Fail(ex.ToString());
     throw new InvalidOperationException(String.Empty, ex);
   }
   {
     string stringTemp = EInteger.Zero.Gcd(BigFromString(
     "244")).ToString();
     Assert.AreEqual(
     "244",
     stringTemp);
   }
   {
     string stringTemp = EInteger.Zero.Gcd(BigFromString(
     "-244")).ToString();
     Assert.AreEqual(
     "244",
     stringTemp);
   }
   {
     string stringTemp = BigFromString(
     "244").Gcd(EInteger.Zero).ToString();
     Assert.AreEqual(
     "244",
     stringTemp);
   }
   {
     string stringTemp = BigFromString(
     "-244").Gcd(EInteger.Zero).ToString();
     Assert.AreEqual(
     "244",
     stringTemp);
   }
   {
     string stringTemp = EInteger.One.Gcd(BigFromString("244")).ToString();
     Assert.AreEqual(
     "1",
     stringTemp);
   }
   {
     string stringTemp = EInteger.One.Gcd(BigFromString(
     "-244")).ToString();
     Assert.AreEqual(
     "1",
     stringTemp);
   }
   {
     string stringTemp = BigFromString("244").Gcd(EInteger.One).ToString();
     Assert.AreEqual(
     "1",
     stringTemp);
   }
   {
     string stringTemp = BigFromString(
     "-244").Gcd(EInteger.One).ToString();
     Assert.AreEqual(
     "1",
     stringTemp);
   }
   {
     string stringTemp = BigFromString("15").Gcd(BigFromString(
     "15")).ToString();
     Assert.AreEqual(
     "15",
     stringTemp);
   }
   {
     string stringTemp = BigFromString("-15").Gcd(
             BigFromString("15")).ToString();
     Assert.AreEqual(
     "15",
     stringTemp);
   }
   {
     string stringTemp = BigFromString("15").Gcd(
             BigFromString("-15")).ToString();
     Assert.AreEqual(
     "15",
     stringTemp);
   }
   {
     string stringTemp = BigFromString(
     "-15").Gcd(BigFromString("-15")).ToString();
     Assert.AreEqual(
     "15",
     stringTemp);
   }
   var prime = 0;
   var rand = new RandomGenerator();
   for (var i = 0; i < 1000; ++i) {
     while (true) {
       prime = rand.UniformInt(0x7fffffff);
       prime |= 1;
       if (IsPrime(prime)) {
         break;
       }
     }
     var bigprime = (EInteger)prime;
     EInteger ba = RandomBigInteger(rand);
     if (ba.IsZero) {
       continue;
     }
     ba *= (EInteger)bigprime;
     Assert.AreEqual(
       bigprime,
       bigprime.Gcd(ba));
   }
   TestGcdPair((EInteger)(-1867), (EInteger)(-4456), EInteger.One);
   TestGcdPair((EInteger)4604, (EInteger)(-4516), (EInteger)4);
   TestGcdPair((EInteger)(-1756), (EInteger)4525, EInteger.One);
   TestGcdPair((EInteger)1568, (EInteger)(-4955), EInteger.One);
   TestGcdPair((EInteger)2519, (EInteger)2845, EInteger.One);
   TestGcdPair((EInteger)(-1470), (EInteger)132, (EInteger)6);
   TestGcdPair((EInteger)(-2982), (EInteger)2573, EInteger.One);
   TestGcdPair((EInteger)(-244), (EInteger)(-3929), EInteger.One);
   TestGcdPair((EInteger)(-3794), (EInteger)(-2325), EInteger.One);
   TestGcdPair((EInteger)(-2667), (EInteger)2123, EInteger.One);
   TestGcdPair((EInteger)(-3712), (EInteger)(-1850), (EInteger)2);
   TestGcdPair((EInteger)2329, (EInteger)3874, EInteger.One);
   TestGcdPair((EInteger)1384, (EInteger)(-4278), (EInteger)2);
   TestGcdPair((EInteger)213, (EInteger)(-1217), EInteger.One);
   TestGcdPair((EInteger)1163, (EInteger)2819, EInteger.One);
   TestGcdPair((EInteger)1921, (EInteger)(-579), EInteger.One);
   TestGcdPair((EInteger)3886, (EInteger)(-13), EInteger.One);
   TestGcdPair((EInteger)(-3270), (EInteger)(-3760), (EInteger)10);
   TestGcdPair((EInteger)(-3528), (EInteger)1822, (EInteger)2);
   TestGcdPair((EInteger)1547, (EInteger)(-333), EInteger.One);
   TestGcdPair((EInteger)2402, (EInteger)2850, (EInteger)2);
   TestGcdPair((EInteger)4519, (EInteger)1296, EInteger.One);
   TestGcdPair((EInteger)1821, (EInteger)2949, (EInteger)3);
   TestGcdPair((EInteger)(-2634), (EInteger)(-4353), (EInteger)3);
   TestGcdPair((EInteger)(-1728), (EInteger)199, EInteger.One);
   TestGcdPair((EInteger)(-4646), (EInteger)(-1418), (EInteger)2);
   TestGcdPair((EInteger)(-35), (EInteger)(-3578), EInteger.One);
   TestGcdPair((EInteger)(-2244), (EInteger)(-3250), (EInteger)2);
   TestGcdPair((EInteger)(-3329), (EInteger)1039, EInteger.One);
   TestGcdPair((EInteger)(-3064), (EInteger)(-4730), (EInteger)2);
   TestGcdPair((EInteger)(-1214), (EInteger)4130, (EInteger)2);
   TestGcdPair((EInteger)(-3038), (EInteger)(-3184), (EInteger)2);
   TestGcdPair((EInteger)(-209), (EInteger)(-1617), (EInteger)11);
   TestGcdPair((EInteger)(-1101), (EInteger)(-2886), (EInteger)3);
   TestGcdPair((EInteger)(-3021), (EInteger)(-4499), EInteger.One);
   TestGcdPair((EInteger)3108, (EInteger)1815, (EInteger)3);
   TestGcdPair((EInteger)1195, (EInteger)4618, EInteger.One);
   TestGcdPair((EInteger)(-3643), (EInteger)2156, EInteger.One);
   TestGcdPair((EInteger)(-2067), (EInteger)(-3780), (EInteger)3);
   TestGcdPair((EInteger)4251, (EInteger)1607, EInteger.One);
   TestGcdPair((EInteger)438, (EInteger)741, (EInteger)3);
   TestGcdPair((EInteger)(-3692), (EInteger)(-2135), EInteger.One);
   TestGcdPair((EInteger)(-1076), (EInteger)2149, EInteger.One);
   TestGcdPair((EInteger)(-3224), (EInteger)(-1532), (EInteger)4);
   TestGcdPair((EInteger)(-3713), (EInteger)1721, EInteger.One);
   TestGcdPair((EInteger)3038, (EInteger)(-2657), EInteger.One);
   TestGcdPair((EInteger)4977, (EInteger)(-110), EInteger.One);
   TestGcdPair((EInteger)(-3305), (EInteger)(-922), EInteger.One);
   TestGcdPair((EInteger)1902, (EInteger)2481, (EInteger)3);
   TestGcdPair((EInteger)(-4804), (EInteger)(-1378), (EInteger)2);
   TestGcdPair((EInteger)(-1446), (EInteger)(-4226), (EInteger)2);
   TestGcdPair((EInteger)(-1409), (EInteger)3303, EInteger.One);
   TestGcdPair((EInteger)(-1626), (EInteger)(-3193), EInteger.One);
   TestGcdPair((EInteger)912, (EInteger)(-421), EInteger.One);
   TestGcdPair((EInteger)751, (EInteger)(-1755), EInteger.One);
   TestGcdPair((EInteger)3135, (EInteger)(-3581), EInteger.One);
   TestGcdPair((EInteger)(-4941), (EInteger)(-2885), EInteger.One);
   TestGcdPair((EInteger)4744, (EInteger)3240, (EInteger)8);
   TestGcdPair((EInteger)3488, (EInteger)4792, (EInteger)8);
   TestGcdPair((EInteger)3632, (EInteger)3670, (EInteger)2);
   TestGcdPair((EInteger)(-4821), (EInteger)(-1749), (EInteger)3);
   TestGcdPair((EInteger)4666, (EInteger)2013, EInteger.One);
   TestGcdPair((EInteger)810, (EInteger)(-3466), (EInteger)2);
   TestGcdPair((EInteger)2199, (EInteger)161, EInteger.One);
   TestGcdPair((EInteger)(-1137), (EInteger)(-1620), (EInteger)3);
   TestGcdPair((EInteger)(-472), (EInteger)66, (EInteger)2);
   TestGcdPair((EInteger)3825, (EInteger)2804, EInteger.One);
   TestGcdPair((EInteger)(-2895), (EInteger)1942, EInteger.One);
   TestGcdPair((EInteger)1576, (EInteger)(-4209), EInteger.One);
   TestGcdPair((EInteger)(-277), (EInteger)(-4415), EInteger.One);
   for (var i = 0; i < 1000; ++i) {
     prime = rand.UniformInt(0x7fffffff);
     if (rand.UniformInt(2) == 0) {
       prime = -prime;
     }
     int intB = rand.UniformInt(0x7fffffff);
     if (rand.UniformInt(2) == 0) {
       intB = -intB;
     }
     var biga = (EInteger)prime;
     var bigb = (EInteger)intB;
     EInteger ba = biga.Gcd(bigb);
     EInteger bb = bigb.Gcd(biga);
     Assert.AreEqual(ba, bb);
   }
 }
Exemplo n.º 23
0
 public void TestToString() {
   var bi = (EInteger)3;
   AssertBigIntegersEqual("3", bi);
   var negseven = (EInteger)(-7);
   AssertBigIntegersEqual("-7", negseven);
   var other = (EInteger)(-898989);
   AssertBigIntegersEqual("-898989", other);
   other = (EInteger)898989;
   AssertBigIntegersEqual("898989", other);
   var r = new RandomGenerator();
   for (var i = 0; i < 1000; ++i) {
     EInteger bigintA = RandomBigInteger(r);
     ExtraTest.TestStringEqualRoundTrip(bigintA);
   }
   // Test serialization of relatively big numbers
   for (var i = 0; i < 20; ++i) {
     EInteger bigintA = RandomBigInteger(r);
     bigintA = bigintA.ShiftLeft(r.UniformInt(2000) + (16 * 500));
     bigintA = bigintA.Subtract(RandomBigInteger(r));
     ExtraTest.TestStringEqualRoundTrip(bigintA);
   }
 }
Exemplo n.º 24
0
 public void TestGetDigitCount() {
   var r = new RandomGenerator();
   {
     object objectTemp = 39;
     object objectTemp2 = EInteger.FromString(
       "101754295360222878437145684059582837272").GetDigitCount();
     Assert.AreEqual(objectTemp, objectTemp2);
   }
   for (var i = 0; i < 1000; ++i) {
     EInteger bigintA = RandomBigInteger(r);
     String str = bigintA.Abs().ToString();
     Assert.AreEqual(str.Length, bigintA.GetDigitCount(), str);
   }
 }
Exemplo n.º 25
0
   public void TestAddSubtract() {
     var r = new RandomGenerator();
     for (var i = 0; i < 10000; ++i) {
       EInteger bigintA = RandomBigInteger(r);
       EInteger bigintB = RandomBigInteger(r);
       EInteger bigintC = bigintA + (EInteger)bigintB;
       EInteger bigintD = bigintC - (EInteger)bigintB;
       if (!bigintD.Equals(bigintA)) {
         Assert.AreEqual(
 bigintA,
 bigintD,
 "TestAddSubtract " + bigintA + "; " + bigintB);
       }
       bigintD = bigintC - (EInteger)bigintA;
       if (!bigintD.Equals(bigintB)) {
         Assert.AreEqual(
 bigintB,
 bigintD,
 "TestAddSubtract " + bigintA + "; " + bigintB);
       }
       bigintC = bigintA - (EInteger)bigintB;
       bigintD = bigintC + (EInteger)bigintB;
       if (!bigintD.Equals(bigintA)) {
         Assert.AreEqual(
 bigintA,
 bigintD,
 "TestAddSubtract " + bigintA + "; " + bigintB);
       }
     }
   }
Exemplo n.º 26
0
 public void TestIsEven() {
   var r = new RandomGenerator();
   for (var i = 0; i < 1000; ++i) {
     EInteger bigintA = RandomBigInteger(r);
     EInteger mod = bigintA.Remainder(BigValueOf(2));
     Assert.AreEqual(mod.IsZero, bigintA.IsEven);
     if (bigintA.IsEven) {
       bigintA = bigintA.Add(EInteger.One);
       Assert.IsTrue(!bigintA.IsEven);
     } else {
       bigintA = bigintA.Add(EInteger.One);
       Assert.IsTrue(bigintA.IsEven);
     }
   }
 }
Exemplo n.º 27
0
 public void TestCompareTo() {
   var r = new RandomGenerator();
   for (var i = 0; i < 500; ++i) {
     EInteger bigintA = RandomBigInteger(r);
     EInteger bigintB = RandomBigInteger(r);
     EInteger bigintC = RandomBigInteger(r);
     TestCommon.CompareTestRelations(bigintA, bigintB, bigintC);
     TestCommon.CompareTestConsistency(bigintA, bigintB, bigintC);
   }
 }
Exemplo n.º 28
0
   public void TestMultiply() {
     try {
       EInteger.One.Multiply(null);
       Assert.Fail("Should have failed");
     } catch (ArgumentNullException) {
       new Object();
     } catch (Exception ex) {
       Assert.Fail(ex.ToString());
       throw new InvalidOperationException(String.Empty, ex);
     }
     var r = new RandomGenerator();
     for (var i = 0; i < 10000; ++i) {
       EInteger bigintA = RandomBigInteger(r);
       EInteger bigintB = bigintA + EInteger.One;
       EInteger bigintC = bigintA * (EInteger)bigintB;
       // Test near-squaring
       if (bigintA.IsZero || bigintB.IsZero) {
         Assert.AreEqual(EInteger.Zero, bigintC);
       }
       if (bigintA.Equals(EInteger.One)) {
         Assert.AreEqual(bigintB, bigintC);
       }
       if (bigintB.Equals(EInteger.One)) {
         Assert.AreEqual(bigintA, bigintC);
       }
       bigintB = bigintA;
       // Test squaring
       bigintC = bigintA * (EInteger)bigintB;
       if (bigintA.IsZero || bigintB.IsZero) {
         Assert.AreEqual(EInteger.Zero, bigintC);
       }
       if (bigintA.Equals(EInteger.One)) {
         Assert.AreEqual(bigintB, bigintC);
       }
       if (bigintB.Equals(EInteger.One)) {
         Assert.AreEqual(bigintA, bigintC);
       }
     }
     DoTestMultiply(
 "39258416159456516340113264558732499166970244380745050",
 "39258416159456516340113264558732499166970244380745051",
 "1541223239349076530208308657654362309553698742116222355477449713742236585667505604058123112521437480247550");
     DoTestMultiply(
 "5786426269322750882632312999752639738983363095641642905722171221986067189342123124290107105663618428969517616421742429671402859775667602123564",
 "331378991485809774307751183645559883724387697397707434271522313077548174328632968616330900320595966360728317363190772921",
 "1917500101435169880779183578665955372346028226046021044867189027856189131730889958057717187493786883422516390996639766012958050987359732634213213442579444095928862861132583117668061032227577386757036981448703231972963300147061503108512300577364845823910107210444");
   }
Exemplo n.º 29
0
 public void TestEquals() {
   Assert.IsFalse(EInteger.One.Equals(null));
   Assert.IsFalse(EInteger.Zero.Equals(null));
   Assert.IsFalse(EInteger.One.Equals(EInteger.Zero));
   Assert.IsFalse(EInteger.Zero.Equals(EInteger.One));
   TestCommon.AssertEqualsHashCode(
     EInteger.Zero,
     EInteger.FromString("-0"));
   TestCommon.AssertEqualsHashCode(
     EInteger.FromString("0"),
     EInteger.FromString("-0"));
   TestCommon.AssertEqualsHashCode(
     EInteger.Zero,
     EInteger.One);
   var r = new RandomGenerator();
   for (var i = 0; i < 1000; ++i) {
     EInteger bigintA = RandomObjects.RandomEInteger(r);
     EInteger bigintB = RandomObjects.RandomEInteger(r);
     TestCommon.AssertEqualsHashCode(bigintA, bigintB);
   }
 }
Exemplo n.º 30
0
 private static EInteger WordAlignedInteger(RandomGenerator r) {
   int size = r.UniformInt(150) + 1;
   EInteger ei = EInteger.Zero;
   for (var i = 0; i < size; ++i) {
     ei = ei.ShiftLeft(16).Add(EInteger.FromInt32(r.UniformInt(0x10000) |
              0x8000));
   }
   return ei;
 }