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

Download:
Math-Random-Discrete-1.00.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 1.00   Source  

NAME ^

Math::Random::Discrete - Discrete random variables with general distributions

VERSION ^

version 1.00

SYNOPSIS ^

    use Math::Random::Discrete;

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

    print $fruit->rand, "\n";

DESCRIPTION ^

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.

METHODS ^

new

    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.

rand

    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

AUTHOR ^

Nick Wellnhofer <wellnhofer@aevum.de>

COPYRIGHT AND LICENSE ^

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: