All files / generator BooleanGenerator.ts

100% Statements 4/4
100% Branches 2/2
100% Functions 2/2
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23                7x 7x                 323x 323x      
import { BooleanOptions } from '../schema/data'
import RNG from '../util/rng'
import { BaseGenerator, type GeneratorOptions } from './BaseGenerator'
 
export default class BooleanGenerator extends BaseGenerator {
  protected rng: RNG
 
  constructor(opts: GeneratorOptions) {
    super(opts)
    this.rng = new RNG(opts.seed)
  }
 
  /**
   * Generates a random boolean value.
   * The probability option defines the likelihood of generating true (0.0 to 1.0).
   * Default probability is 0.5 (equal chance of true or false).
   */
  generate(options: Partial<BooleanOptions>): boolean {
    const probability = options.probability ?? 0.5
    return this.rng.uniformFloat(0, 1) < probability
  }
}