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


Data-RandomPerson documentation  | view source Contained in the Data-RandomPerson distribution.

Index


NAME

Top

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

VERSION

Top

This document refers to version 0.4 of Data::RandomPerson::Choice, released Sept 13th, 2005

SYNOPSIS

Top

  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

Top

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

Data::RandomPerson::Choice->new( )

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

size( )

Returns the size of the list so far

pick()

Returns an element from the list.

ENVIRONMENT

Top

Perl 5

DIAGNOSTICS

Top

No data has been added to the list ...

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

BUGS

Top

None so far

FILES

Top

None

SEE ALSO

Top

None

AUTHOR

Top

Peter Hickman (peterhi@ntlworld.com)

COPYRIGHT

Top


Data-RandomPerson documentation  | view source Contained in the Data-RandomPerson distribution.