Data::RandomPerson::Choice - Select an element from a list in proportion


  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();



A way of simply defining the probability 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


Returns a Data::RandomPerson::Choice object.

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


Returns the size of the list so far


Returns an element from the list.


No data has been added to the list ...

This error will occur if you have called pick() before add() has been called.


Peter Hickman (


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.