Repository: motion-canvas/motion-canvas
File: packages/core/src/scenes/Random.ts (TypeScript)

Code before:
   *
   * @param from - The start of the range.
   * @param to - The end of the range.
   */
  public nextFloat(from = 0, to = 1) {
    return map(from, to, this.next());
  }

  /**
   * Get the next random integer in the given range.
   *
   * @param from - The start of the range.
   * @param to - The end of the range. Exclusive.
   */
  public nextInt(from = 0, to = 4294967296) {
    let value = Math.floor(map(from, to, this.next()));
    if (value === to) {
      value = from;
    }

    return value;
  }

  // This is an optimization.
  // Since gauss() generates a pair of independent Gaussian random numbers,
  // it returns one immediately and stores the other for the next call to gauss().
  private nextGauss: number | null = null;

  /**
   * Get a random float from a gaussian distribution.
   * @param mean - The mean of the distribution.
   * @param stdev - The standard deviation of the distribution.
   */
  public gauss(mean = 0.0, stdev = 1.0): number {
    let z = this.nextGauss;
    this.nextGauss = null;
    if (z === null) {
      const x2pi = this.next() * 2 * Math.PI;
      const g2rad = Math.sqrt(-2.0 * Math.log(1.0 - this.next()));
      z = Math.cos(x2pi) * g2rad;
      this.nextGauss = Math.sin(x2pi) * g2rad;
    }
    return mean + z * stdev;
  }

  /**
   * Get an array filled with random floats in the given range.
   *
   * @param size - The size of the array.
   * @param from - The start of the range.
   * @param to - The end of the range.

Review comment (suggestion):
I think these remarks should be written as TSDoc:
```suggestion
  /**
   * Previously generated Gaussian random number.
   *
   * @remarks
   * This is an optimization.
   * Since {@link gauss} generates a pair of independent Gaussian random
   * numbers, it returns one immediately and stores the other for the next call
   * to {@link gauss}.
   */
```

Code after:
  public constructor(private state: number) {}

  /**
   * @internal
   */
  public static createSeed() {
    return Math.floor(Math.random() * 4294967296);
  }

  /**
   * Get the next random float in the given range.
   *
   * @param from - The start of the range.
   * @param to - The end of the range.
   */
  public nextFloat(from = 0, to = 1) {
    return map(from, to, this.next());
  }

  /**
   * Get the next random integer in the given range.
   *
   * @param from - The start of the range.
   * @param to - The end of the range. Exclusive.
   */
  public nextInt(from = 0, to = 4294967296) {
    let value = Math.floor(map(from, to, this.next()));
    if (value === to) {
      value = from;
    }

    return value;
  }

  /**
   * Get a random float from a gaussian distribution.
   * @param mean - The mean of the distribution.
   * @param stdev - The standard deviation of the distribution.
   */
  public gauss(mean = 0, stdev = 1): number {
    let z = this.nextGauss;
    this.nextGauss = null;
    if (z === null) {
      const x2pi = this.next() * 2 * Math.PI;
      const g2rad = Math.sqrt(-2 * Math.log(1 - this.next()));
      z = Math.cos(x2pi) * g2rad;
      this.nextGauss = Math.sin(x2pi) * g2rad;
    }
    return mean + z * stdev;
  }