|
| 1 | +package com.upokecenter.test; |
| 2 | +/* |
| 3 | +Written by Peter O. |
| 4 | +Any copyright to this work is released to the Public Domain. |
| 5 | +In case this is not possible, this work is also |
| 6 | +licensed under the Unlicense: https://unlicense.org/ |
| 7 | +
|
| 8 | + */ |
| 9 | + |
| 10 | +import com.upokecenter.util.*; |
| 11 | +import com.upokecenter.numbers.*; |
| 12 | + |
| 13 | + /** |
| 14 | + * Generates random objects of various kinds for purposes of testing code that |
| 15 | + * uses them. The methods will not necessarily sample uniformly from all |
| 16 | + * objects of a particular kind. |
| 17 | + */ |
| 18 | + public final class RandomNumerics { |
| 19 | +private RandomNumerics() { |
| 20 | +} |
| 21 | + private static final int MaxExclusiveStringLength = 0x2000; |
| 22 | + private static final int MaxExclusiveShortStringLength = 50; |
| 23 | + private static final int MaxNumberLength = 50000; |
| 24 | + private static final int MaxShortNumberLength = 40; |
| 25 | + |
| 26 | + public static ERational RandomERational(IRandomGenExtended rand) { |
| 27 | + EInteger bigintA = RandomEInteger(rand); |
| 28 | + EInteger bigintB = RandomEInteger(rand); |
| 29 | + if (bigintB.isZero()) { |
| 30 | + bigintB = EInteger.FromInt32(1); |
| 31 | + } |
| 32 | + return ERational.Create(bigintA, bigintB); |
| 33 | + } |
| 34 | + |
| 35 | + public static EDecimal GenerateEDecimalSmall(IRandomGenExtended wrapper) { |
| 36 | + if (wrapper == null) { |
| 37 | + throw new NullPointerException("wrapper"); |
| 38 | + } |
| 39 | + if (wrapper.GetInt32(2) == 0) { |
| 40 | + EInteger eix = EInteger.FromBytes( |
| 41 | + RandomObjects.RandomByteString(wrapper, 1 + wrapper.GetInt32(36)), |
| 42 | + true); |
| 43 | + int exp = wrapper.GetInt32(25) - 12; |
| 44 | + return EDecimal.Create(eix, exp); |
| 45 | + } |
| 46 | + return |
| 47 | +EDecimal.FromString(RandomObjects.RandomDecimalStringShort(wrapper, false)); |
| 48 | + } |
| 49 | + |
| 50 | + public static EDecimal RandomEDecimal(IRandomGenExtended r) { |
| 51 | + return RandomEDecimal(r, null); |
| 52 | + } |
| 53 | + |
| 54 | + public static EDecimal RandomEDecimal(IRandomGenExtended r, String[] |
| 55 | + decimalString) { |
| 56 | + if (r == null) { |
| 57 | + throw new NullPointerException("r"); |
| 58 | + } |
| 59 | + if (r.GetInt32(100) == 0) { |
| 60 | + int x = r.GetInt32(3); |
| 61 | + if (x == 0) { |
| 62 | + if (decimalString != null) { |
| 63 | + decimalString[0] = "Infinity"; |
| 64 | + } |
| 65 | + return EDecimal.PositiveInfinity; |
| 66 | + } |
| 67 | + if (x == 1) { |
| 68 | + if (decimalString != null) { |
| 69 | + decimalString[0] = "-Infinity"; |
| 70 | + } |
| 71 | + return EDecimal.NegativeInfinity; |
| 72 | + } |
| 73 | + if (x == 2) { |
| 74 | + if (decimalString != null) { |
| 75 | + decimalString[0] = "NaN"; |
| 76 | + } |
| 77 | + return EDecimal.NaN; |
| 78 | + } |
| 79 | + // Signaling NaN currently not generated because |
| 80 | + // it doesn't round-trip as well |
| 81 | + } |
| 82 | + if (r.GetInt32(100) < 30) { |
| 83 | + String str = RandomObjects.RandomDecimalString(r); |
| 84 | + if (str.length() < 500) { |
| 85 | + if (decimalString != null) { |
| 86 | + decimalString[0] = str; |
| 87 | + } |
| 88 | + return EDecimal.FromString(str); |
| 89 | + } |
| 90 | + } |
| 91 | + EInteger emant = RandomEInteger(r); |
| 92 | + EInteger eexp; |
| 93 | + if (r.GetInt32(100) < 95) { |
| 94 | + int exp = (r.GetInt32(100) < 80) ? (r.GetInt32(50) - 25) : |
| 95 | + (r.GetInt32(5000) - 2500); |
| 96 | + eexp = EInteger.FromInt32(exp); |
| 97 | + } else { |
| 98 | + eexp = RandomEInteger(r); |
| 99 | + } |
| 100 | + EDecimal ed = EDecimal.Create(emant, eexp); |
| 101 | + if (decimalString != null) { |
| 102 | + decimalString[0] = emant.toString() + "E" + eexp.toString(); |
| 103 | + } |
| 104 | + return ed; |
| 105 | + } |
| 106 | + |
| 107 | + private static EInteger BitHeavyEInteger(IRandomGenExtended rg, int count) { |
| 108 | + StringBuilder sb = new StringBuilder(); |
| 109 | + int[] oneChances = { |
| 110 | + 999, 1, 980, 20, 750, 250, 980, |
| 111 | + 20, 980, 20, 980, 20, 750, 250, |
| 112 | + }; |
| 113 | + int oneChance = oneChances[rg.GetInt32(oneChances.length)]; |
| 114 | + for (int i = 0; i < count; ++i) { |
| 115 | + sb.append((rg.GetInt32(1000) >= oneChance) ? '0' : '1'); |
| 116 | + } |
| 117 | + return EInteger.FromRadixString(sb.toString(), 2); |
| 118 | + } |
| 119 | + |
| 120 | + private static EInteger DigitHeavyEInteger(IRandomGenExtended rg, int |
| 121 | + count) { |
| 122 | + StringBuilder sb = new StringBuilder(); |
| 123 | + int[] oneChances = { |
| 124 | + 999, 1, 980, 20, 750, 250, 980, |
| 125 | + 20, 980, 20, 980, 20, 750, 250, |
| 126 | + }; |
| 127 | + int oneChance = oneChances[rg.GetInt32(oneChances.length)]; |
| 128 | + for (int i = 0; i < count; ++i) { |
| 129 | + sb.append((rg.GetInt32(1000) >= oneChance) ? '0' : '9'); |
| 130 | + } |
| 131 | + return EInteger.FromRadixString(sb.toString(), 10); |
| 132 | + } |
| 133 | + |
| 134 | + public static EInteger RandomEInteger(IRandomGenExtended r) { |
| 135 | + if (r == null) { |
| 136 | + throw new NullPointerException("r"); |
| 137 | + } |
| 138 | + int selection = r.GetInt32(100); |
| 139 | + if (selection < 10) { |
| 140 | + int count = r.GetInt32(MaxNumberLength); |
| 141 | + count = (int)((long)count * r.GetInt32(MaxNumberLength) / |
| 142 | + MaxNumberLength); |
| 143 | + count = (int)((long)count * r.GetInt32(MaxNumberLength) / |
| 144 | + MaxNumberLength); |
| 145 | + count = Math.max(count, 1); |
| 146 | + if (selection == 0 || selection == 1) { |
| 147 | + return BitHeavyEInteger(r, count); |
| 148 | + } else if ((selection == 2 || selection == 3) && count < 500) { |
| 149 | + return DigitHeavyEInteger(r, count); |
| 150 | + } |
| 151 | + byte[] bytes = RandomObjects.RandomByteString(r, count); |
| 152 | + return EInteger.FromBytes(bytes, true); |
| 153 | + } else { |
| 154 | + byte[] bytes = RandomObjects.RandomByteString( |
| 155 | + r, |
| 156 | + r.GetInt32(MaxShortNumberLength) + 1); |
| 157 | + return EInteger.FromBytes(bytes, true); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + public static EInteger RandomEIntegerSmall(IRandomGenExtended r) { |
| 162 | + if (r == null) { |
| 163 | + throw new NullPointerException("r"); |
| 164 | + } |
| 165 | + byte[] bytes = RandomObjects.RandomByteString( |
| 166 | + r, |
| 167 | + r.GetInt32(MaxShortNumberLength) + 1); |
| 168 | + return EInteger.FromBytes(bytes, true); |
| 169 | + } |
| 170 | + |
| 171 | + private static int IntInRange(IRandomGenExtended rg, int minInc, int |
| 172 | + maxExc) { |
| 173 | + return minInc + rg.GetInt32(maxExc - minInc); |
| 174 | + } |
| 175 | + |
| 176 | + public static EFloat CloseToPowerOfTwo(IRandomGenExtended rg) { |
| 177 | + if (rg == null) { |
| 178 | + throw new NullPointerException("rg"); |
| 179 | + } |
| 180 | + int pwr = (rg.GetInt32(100) < 80) ? IntInRange(rg, -20, 20) : |
| 181 | + IntInRange(rg, -300, 300); |
| 182 | + int pwr2 = pwr - (rg.GetInt32(100) < 80 ? IntInRange(rg, 51, 61) : |
| 183 | + IntInRange(rg, 2, 300)); |
| 184 | + EFloat ef = rg.GetInt32(2) == 0 ? EFloat.Create(1, |
| 185 | + pwr).Add(EFloat.Create(1, pwr2)) : EFloat.Create(1, |
| 186 | + pwr).Subtract(EFloat.Create(1, pwr2)); |
| 187 | + if (rg.GetInt32(10) == 0) { |
| 188 | + pwr2 = pwr - (rg.GetInt32(100) < 80 ? IntInRange(rg, 51, 61) : |
| 189 | + IntInRange(rg, 2, 300)); |
| 190 | + ef = (rg.GetInt32(2) == 0) ? ef.Add(EFloat.Create(1, pwr2)) : |
| 191 | + ef.Subtract(EFloat.Create(1, pwr2)); |
| 192 | + } |
| 193 | + return ef; |
| 194 | + } |
| 195 | + |
| 196 | + public static EFloat RandomEFloat(IRandomGenExtended r) { |
| 197 | + if (r == null) { |
| 198 | + throw new NullPointerException("r"); |
| 199 | + } |
| 200 | + if (r.GetInt32(100) == 0) { |
| 201 | + int x = r.GetInt32(3); |
| 202 | + if (x == 0) { |
| 203 | + return EFloat.PositiveInfinity; |
| 204 | + } |
| 205 | + if (x == 1) { |
| 206 | + return EFloat.NegativeInfinity; |
| 207 | + } |
| 208 | + if (x == 2) { |
| 209 | + return EFloat.NaN; |
| 210 | + } |
| 211 | + } |
| 212 | + return r.GetInt32(100) == 3 ? |
| 213 | + CloseToPowerOfTwo(r) : EFloat.Create( |
| 214 | + RandomEInteger(r), |
| 215 | + EInteger.FromInt64(r.GetInt32(400) - 200)); |
| 216 | + } |
| 217 | + |
| 218 | + public static EInteger RandomSmallIntegral(IRandomGenExtended r) { |
| 219 | + return EInteger.FromString(RandomObjects.RandomSmallIntegralString(r)); |
| 220 | + } |
| 221 | + } |
0 commit comments