weighted_random_picker 1.0.0 copy "weighted_random_picker: ^1.0.0" to clipboard
weighted_random_picker: ^1.0.0 copied to clipboard

The elements of the array given to the constructor can be randomly picked with weights according to the probabilityWeight property of each element.

example/weighted_random_picker_example.dart

import 'package:weighted_random_picker/weighted_random_picker.dart';

class Entity implements WeightedRandomPickable {
  final String id;
  @override
  final double probabilityWeight;
  const Entity(this.id, this.probabilityWeight);
}

void main() {
  final List<Entity> entities = [
    const Entity('a', 1.1),
    const Entity('b', 1.1),
    const Entity('c', 0.9),
    const Entity('d', 0.3),
    const Entity('e', 0.1),
  ];

  final picker = WeightedRandomPicker(entities);

  print(picker.pick().id);

  entities[2] = Entity('c', 0.5);
  picker.update(2);

  print(picker.pick().id);
}
0
likes
150
points
4
downloads

Publisher

verified publishermalt03.com

Weekly Downloads

The elements of the array given to the constructor can be randomly picked with weights according to the probabilityWeight property of each element.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on weighted_random_picker