probabilisticErrorMasks function

List<LogicValue> probabilisticErrorMasks(
  1. int width, {
  2. required int count,
  3. double probability = 0.5,
  4. int seed = 1,
})

Generates a deterministic pseudo-random sequence of error masks.

This is a test-vector generator, not a hardware random source. A zero seed is replaced with a fixed nonzero state so the sequence remains reproducible.

Implementation

List<LogicValue> probabilisticErrorMasks(
  int width, {
  required int count,
  double probability = 0.5,
  int seed = 1,
}) {
  _validateWidth(width);
  if (count < 0 || probability < 0 || probability > 1) {
    throw RohdHclException(
        'count must be non-negative and probability must be in [0, 1].');
  }
  var state = seed & 0x7fffffff;
  if (state == 0) {
    state = 0x1;
  }
  final result = <LogicValue>[];
  for (var sample = 0; sample < count; sample++) {
    var mask = BigInt.zero;
    for (var bit = 0; bit < width; bit++) {
      state ^= (state << 13) & 0x7fffffff;
      state ^= state >> 17;
      state ^= (state << 5) & 0x7fffffff;
      final normalized = state / 0x7fffffff;
      if (normalized < probability) {
        mask |= BigInt.one << bit;
      }
    }
    result.add(LogicValue.ofBigInt(mask, width));
  }
  return result;
}