Skip to content
pubscout

dart_random_choice

0.2.0, released 4 years ago by kirocodes.dev

Select a random element from any iterable, optionally using weights.

WASM-ready
$ flutter pub add dart_random_choice
Source repository

What it does

The package exposes a generic randomChoice<T> function that returns a randomly selected item from an iterable. You can provide a parallel list of numeric weights to bias the selection.

AI-written summary of version 0.2.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need a quick, dependency‑free way to pick a random item from a list or set
  • You want weighted random selection without pulling in larger libraries

Skip it when

  • Cryptographically secure randomness is required
  • You need asynchronous streams or reactive selection

Strengths

  • Zero external dependencies
  • Null‑safe and works on all Dart platforms
  • Supports weighted selection

Weaknesses

  • Uses dart:math random, not suitable for security‑critical use
  • No async or stream support
  • Limited to simple weight lists

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
0
import 'package:dart_random_choice/dart_random_choice.dart';

void main() {
  const foods = ['burger','pizza','salad'];
  const weights = [10,20,0.4];
  var choice = randomChoice<String>(foods, weights);
  print(choice);
}

Alternatives

Packages that do the same job and could replace dart_random_choice.