import 'dart:math'; class PseudoRandom { int a; int c; int m = 1 << 32; int s; int i; PseudoRandom([int prod = 1664525, int add = 1013904223]) { a = prod; c = add; s = Random().nextInt(m) * 2 + 1;//odd next();// a fast round i = a.modInverse(m);//4276115653 as inverse of 1664525 } int next() { return s = (a * s + c) % m; } int prev() { return s = (s - c) * i % m; } } class RingNick { List<double> walls = [ 0.25, 0.5, 0.75 ]; int position = 0; int mostEscaped = 1;//the lowest pair of walls 0.25 and 0.5 int leastEscaped = 2;//the highest walls 0.5 and 0.75 int theThird = 0;//the 0.75 and 0.25 walls bool right = true; PseudoRandom pr = PseudoRandom(); int _getPosition() => position; int _asMod(int pos) { return pos % walls.length; } void _setPosition(int pos) { position = _asMod(pos); } void _next() { int direction = right ? 0 : walls.length - 1;//truncate to 2 double wall = walls[_asMod(_getPosition() + direction)]; if(pr.next() > (wall * pr.m).toInt()) { //jumped _setPosition(position + (right ? 1 : walls.length - 1)); } else { //not jumped right = !right;//bounce } } void _prev() { int direction = !right ? 0 : walls.length - 1;//truncate to 2 double wall = walls[_asMod(_getPosition() + direction)]; if(pr.s > (wall * pr.m).toInt()) {// the jump over before sync //jumped _setPosition(position + (!right ? 1 : walls.length - 1)); } else { //not jumped right = !right;//bounce -- double bounce and scale before sync } pr.prev();//exact inverse } void next() { _next(); while(_getPosition() == mostEscaped) _next(); } void prev() { _prev(); while(_getPosition() == mostEscaped) _prev(); } } class GroupHandler { List<RingNick> rn; GroupHandler(int size) { if(size % 2 == 0) size++; rn = List<RingNick>(size); } void next() { for(RingNick r in rn) r.next(); } void prev() { for(RingNick r in rn.reversed) r.prev(); } bool majority() { int count = 0; for(RingNick r in rn) if(r._getPosition() == r.leastEscaped) count++;//a main cumulative return (2 * count > rn.length);// the > 2/3rd state is true } void modulate() { for(RingNick r in rn) if(r._getPosition() == r.leastEscaped) { r._setPosition(r.theThird); } else { //mostEscaped eliminated by not being used r._setPosition(r.leastEscaped); } } } class Modulator { GroupHandler gh = GroupHandler(55); int putBit(bool bitToAbsorb) {//returns absorption status gh.next(); if(gh.majority()) {//main zero state if(bitToAbsorb) { gh.modulate(); return 0;//a zero yet to absorb } else { return 1;//absorbed zero } } else { return -1;//no absorption emitted 1 } } int getBit(bool bitLastEmitted) { if(gh.majority()) {//zero gh.prev(); return 1;//last bit not needed emit zero } else { if(bitLastEmitted) { gh.prev(); return -1;//last bit needed and nothing to emit } else { gh.modulate(); gh.prev(); return 0;//last bit needed, emit 1 } } } } class StackHandler { List<bool> data = []; Modulator m = Modulator(); int putBits() { int count = 0; while(data.length > 0) { bool v = data.removeLast(); switch(m.putBit(v)) { case -1: data.add(v); data.add(true); break; case 0: data.add(false); break; case 1: break;//absorbed zero default: break; } count++; } return count; } void getBits(int count) { while(count > 0) { bool v; v = (data.length == 0 ? false : data.removeLast());//zeros out switch(m.getBit(v)) { case 1: data.add(v);//not needed data.add(false);//emitted zero break; case 0: data.add(true);//emitted 1 used zero break; case -1: break;//bad skip, ... default: break; } count--; } } }
One thought on “Today’s Thought”
Comments are closed.
void next() {
for(int i = 0; i < window; i++) for(RingNick r in rn) r.next(); } void prev() { for(int i = 0; i < window; i++) for(RingNick r in rn.reversed) r.prev(); }