Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- r = a + binary2dec(flip n times write 0 for heads and 1 for tails)
- n = b-a;
- N = round_to_next_larger_power_of_2(n)
- while (1) {
- x = random(0 included to N excluded);
- if (x < n) break;
- }
- r = a + x;
- public static IEnumerable<BigInteger> DigitConversion(this IEnumerable<BigInteger> inputStream, BigInteger modIn, BigInteger modOut) {
- //note: values are implicitly scaled so the first unfixed digit of the output ranges from 0 to 1
- Rational b = 0; //offset of the chosen range
- Rational d = 1; //size of the chosen range
- foreach (var r in inputStream) {
- //narrow the chosen range towards the real value represented by the input
- d /= modIn;
- b += d * r;
- //check for output digits that have become fixed
- while (true) {
- var i1 = (b * modOut).Floor();
- var i2 = ((b + d) * modOut).Floor(); //note: ideally b+d-epsilon, but another iteration makes that correction unnecessary
- if (i1 != i2) break; //digit became fixed?
- //fix the next output digit (rescale the range to make next digit range from 0 to 1)
- d *= modOut;
- b *= modOut;
- b -= i1;
- yield return i1;
- }
- }
- }
- Keep a random seed s and initialize with s = R(m).
- function random [0, z-1] :
- x = R(m) + s
- while x >= z:
- x -= z
- s = x
- return x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement