This is a random number generator that does not need an external extension. It uses pure perl only.

package RANDOM; use 5.006; use strict; use warnings; require Exporter; use AutoLoader qw(AUTOLOAD); our @ISA = qw(Exporter); our @EXPORT = qw( random ); our $VERSION = '0.01'; sub new { return bless {}, shift; } sub random { my $self = shift; return rand(); } 1; __END__ =head1 NAME RANDOM - Perl random number generator =head1 SYNOPSIS use RANDOM; my $randomizer = new RANDOM; my $randomnumber = $randomizer->random(); =head1 DESCRIPTION Applies random number generation in pure perl. No external extension needed. =head1 AUTHOR Chady K., E<lt>chady@perlmonk.orgE<gt> =head1 SEE ALSO L<perl>. =cut

Replies are listed 'Best First'.
Re: Random number generator
by grantm (Parson) on Apr 01, 2003 at 08:32 UTC

    I think you could improve the efficiency of this module a little. Your constructor creates a hash but then doesn't store anything in it. Might I suggest replacing this line:

    return bless {}, shift;

    with this:

    return bless \undef, shift;

    I'm also wondering if maybe the motivation for this module is related to the posting date :-)

    Update: Yes, I know if you tried that in real life you'd get a 'Modification of a read-only value attempted' error - some people are taking this far too seriously :-)

      That a really neat idea.

      As \undef always returns the same value regardless of how many times you call it within a given script, that will effectively turn the module into a singleton. This has the very desirable side-effect of ensuring that the random sequence isn't messed by calling multiple instances. It neatly removes the need to serialise the access.


      Examine what is said, not who speaks.
      1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
      2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
      3) Any sufficiently advanced technology is indistinguishable from magic.
      Arthur C. Clarke.
        It also removes the only reason why you'd want to use an OO wrapper around random: the ability to subclass it, and have parts of the program use a different random function. If you turn it into a singleton, you might as well just use rand directly.

        Abigail

      Your constructor creates a hash but then doesn't store anything in it.
      A really useful object oriented random module would have each object have their own, independent, pseudo-random generator. Instead, they all share and use the underlying common one that's built into Perl.

      But then, writing a module because it then no longer needs anything but perl... There's a flaw in that logic, but I can't quite put my finger on it...

        There's a flaw allright.. but it's not in the logic. It's in the date ;)


        He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

        Chady | http://chady.net/
Re: Random number generator
by Coruscate (Sexton) on Apr 01, 2003 at 08:19 UTC

    Can I ask why this is in a module? It doesn't do anything besides make a round-about way of calling rand()... Perhaps adding functionality of passing ranges for the numbers to be picked from would improve this. Not to mention that this is a complete waste of the purpose of OO programming. :(

    Other than that, here's the way I replace your whole module:

    #!/usr/bin/perl -w use strict; my $num = rand();


    Update: If this is simply your first try at creating a module, then I suppose I'd say you accomplished that nicely. You know how to export functions and do a couple of small things, so good job. Now all you need to learn is how to keep data within an object (separate from other objects) and how to manipulate this data. Good start though :P


    If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

      you probably need to look at the date of the post - read (April's Fool?)

      ;)
      He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

      Chady | http://chady.net/

        Yeah, my reply was an april fool's too. Didn'cha know?

        Okay, so it wasn't lol. I noticed the date thing posted by someone else and it hit me like a sack of wet potatos. :) I can be such a sucker sometimes. I 70% fell for the cpan.org joke too.


        If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.