NAME
Data::RandomPerson::Choice - Select an element from a list in proportion
SYNOPSIS
use Data::RandomPerson::Choice;
my $c = Data::RandomPerson::Choice->new();
$c->add( 'this' );
$c->add( 'that', 11 );
# The list now contains 12 elements, 1 'this' and 11 'that'. A random
# element will be selected with the following probablilty:
#
# this = 1/12 = 0.083
# that = 11/12 = 0.917
print $c->pick();
DESCRIPTION
Overview
A way of simply defining the probablity of a selection of values based on the ratio of the elements this add( 'this' ) adds one element to the list, the count argument of add defaults to 1. add( 'that', 11 ) adds another 11 elements to the list giving a total of 12. The chance of picking 'this' is 1 in 12 and the change for 'that' is 11 in 12.
You can add as many values as you like to the list.
Constructors and initialization
Class and object methods
- add( VALUE [, COUNT] )
-
Adds VALUE to the list COUNT times, if COUNT is omitted it will default to 1.
- add_list( LIST )
-
Adds a LIST of items to the data
- size( )
-
Returns the size of the list so far
- pick()
-
Returns an element from the list.
DIAGNOSTICS
- No data has been added to the list ...
-
This error will occur if you have called pick() before add() has been called.
AUTHOR
Peter Hickman (peterhi@ntlworld.com)
COPYRIGHT
Copyright (c) 2005, Peter Hickman. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.