Nick Wellnhofer >
Math-Random-Discrete-1.00 >
Math::Random::Discrete

Module Version: 1.00
Math::Random::Discrete - Discrete random variables with general distributions

version 1.00

use Math::Random::Discrete; my $fruit = Math::Random::Discrete->new( [ 40, 20, 10 ], [ 'Apple', 'Orange', 'Banana' ], ); print $fruit->rand, "\n";

Math::Random::Discrete generates discrete random variables according to a user-defined distribution. It uses Walker's alias method to create random values in O(1) time.

my $generator = Math::Random::Discrete->new(\@weights, \@items);

Creates a random generator for the distribution given by values in @weights. These values can be probabilities, frequencies or any kind of weights. They don't have to add up to 1. @items is an array of items corresponding to the weights. If it is omitted, numbers 0, 1, 2, ... are used.

my $item = $generator->rand;

Returns a random item according to the given distribution. That is, item i is returned with probability

p[i] = weight[i] / sum_of_all_weights

Nick Wellnhofer <wellnhofer@aevum.de>

This software is copyright (c) 2011 by Nick Wellnhofer.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

syntax highlighting: