revised random-bit generation algorithm

This commit is contained in:
Christopher A. Mosher
2018-12-23 12:08:36 -05:00
parent 57f9dc3045
commit b531bc96f5

View File

@@ -32,7 +32,7 @@ private:
StepperMotor& arm; StepperMotor& arm;
bool pulse; bool pulse;
std::uint8_t cContiguousZeroBits; std::uint8_t bitBufferRead;
@@ -48,7 +48,7 @@ public:
disk(disk), disk(disk),
arm(arm), arm(arm),
pulse(false), pulse(false),
cContiguousZeroBits(0), bitBufferRead(0),
generator(std::chrono::system_clock::now().time_since_epoch().count()), generator(std::chrono::system_clock::now().time_since_epoch().count()),
distribution(0,1) { distribution(0,1) {
} }
@@ -93,18 +93,12 @@ public:
void rotateDiskOneBit() { void rotateDiskOneBit() {
this->disk.rotateOneBit(this->arm.getQuarterTrack()); this->disk.rotateOneBit(this->arm.getQuarterTrack());
if (this->disk.getBit(this->arm.getQuarterTrack())) { bitBufferRead <<= 1;
this->pulse = true; bitBufferRead |= this->disk.getBit(this->arm.getQuarterTrack());
cContiguousZeroBits = 0; if (bitBufferRead & 0x0Fu) {
this->pulse = (bitBufferRead & 0x02u) >> 1;
} else { } else {
// keep a count of contiguous zero-bits and generate random bits when this->pulse = randomBit();
// we see more than three (emulating the MC3470, see UA2, 9-11)
++cContiguousZeroBits;
if (3 < cContiguousZeroBits) {
if (randomBit()) {
this->pulse = true;
}
}
} }
} }