001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package org.wpilib.math.random;
006
007import java.util.Random;
008import org.ejml.simple.SimpleMatrix;
009import org.wpilib.math.linalg.Matrix;
010import org.wpilib.math.numbers.N1;
011import org.wpilib.math.util.Num;
012
013/** Utilities for generating normally distributed random values. */
014public final class Normal {
015  private static Random rand = new Random();
016
017  private Normal() {
018    throw new UnsupportedOperationException("This is a utility class!");
019  }
020
021  /**
022   * Creates a vector of normally distributed random values with the given standard deviations for
023   * each element.
024   *
025   * @param <N> Num representing the dimensionality of the noise vector to create.
026   * @param stdDevs A matrix whose elements are the standard deviations of each element of the
027   *     random vector.
028   * @return Vector of normally distributed values.
029   */
030  public static <N extends Num> Matrix<N, N1> normal(Matrix<N, N1> stdDevs) {
031    Matrix<N, N1> result = new Matrix<>(new SimpleMatrix(stdDevs.getNumRows(), 1));
032    for (int i = 0; i < stdDevs.getNumRows(); i++) {
033      result.set(i, 0, rand.nextGaussian() * stdDevs.get(i, 0));
034    }
035    return result;
036  }
037}