WeightRandom<T> constructor

WeightRandom<T>(
  1. Iterable<WeightObj<T>> weights
)

Implementation

WeightRandom(Iterable<WeightObj<T>> weights) : _weights = List.of(weights) {
  var total = _weights.fold(0.0, (sum, w) => sum + w.weight);
  _cumulativeWeights = _weights
      .map((w) => w.weight / total)
      .fold<List<double>>(
    [],
        (list, w) => list..add((list.isEmpty ? 0 : list.last) + w),
  );
}