randchoice 0.0.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 0

randchoice - Pick random choices from lists.

A micro library for picking random item(s) from lists.

The population of lists can be weighted if required.

Inspired by Python's random.choices.

Usage Lists

A simple usage example:

main() {
  print('\nExample 1 - Pick 1 from a list.');
  print(RandomChoice.choices(['a', 'b', 'c'])[0]);

  print('\nExample 2 - Pick 2 from a list.');
  var results = RandomChoice.choices(['red', 'green', 'blue', 'alpha'], 2);
  print(results[0]);
  print(results[1]);

  print('\nExample 3 - Pick 10 from a list and ');
  print('weight the elephants to be twice as likely.');
  results = RandomChoice.choices(['elephant', 'frog'], 10, [2, 1]);
  results.forEach((String animal) => print('\t$animal'));

  print('\nExample 4 - Pick 10 from a list and ');
  print('weight the elephants to be four times as likely.');
  results = RandomChoice.choices(['elephant', 'frog'], 10, [4, 1]);
  results.forEach((String animal) => print('\t$animal'));
}

Features and bugs

Please file feature requests and bugs at the issue tracker.

Changelog

0.0.1

  • First package release of choices and weighted choices.

example/randchoice_example.dart

// Copyright (c) 2017, daftspaniel. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'package:randchoice/randchoice.dart';

main() {
  print('\nExample 1 - Pick 1 from a list.');
  print(RandomChoice.choices(['a', 'b', 'c'])[0]);
  print(RandomChoice.choices([1, 2, 3])[0]);

  print('\nExample 2 - Pick 2 from a list.');
  var results = RandomChoice.choices(['red', 'green', 'blue', 'alpha'], 2);
  print(results[0]);
  print(results[1]);

  print('\nExample 3 - Pick 10 from a list and ');
  print('weight the elephants to be twice as likely.');
  results = RandomChoice.choices(['elephant', 'frog'], 10, [2, 1]);
  results.forEach((String animal) => print('\t$animal'));

  print('\nExample 4 - Pick 10 from a list and ');
  print('weight the elephants to be four times as likely.');
  results = RandomChoice.choices(['elephant', 'frog'], 10, [4, 1]);
  results.forEach((String animal) => print('\t$animal'));
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  randchoice: ^0.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:randchoice/randchoice.dart';
  
Version Uploaded Documentation Archive
0.0.1 Sep 3, 2017 Go to the documentation of randchoice 0.0.1 Download randchoice 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
0
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.24.2 <2.0.0-dev.infinity