Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm working with a PDL and I would like to initialize my pdl as 100x100 with random values in the range of 1-5.
I've found that $a = random(100,100); will give me a 100x100 piddle with random values from [0-1). I also found randsym, which will give (0-1). Is there a way to use these for a range of values (random within 0-5), or am I stuck doing a loop with set's and the perl rand($range); standard?
Thanks in advanced.

Replies are listed 'Best First'.
Re: Random Values (1-5) for a PDL?
by syphilis (Archbishop) on Mar 06, 2009 at 02:05 UTC
    I would like to initialize my pdl as 100x100 with random values in the range of 1-5

    I don't know if it can be done in one line. Would the following be a satisfactory workaround:
    $a = random(100, 100); $a *= 4; $a += 1;
    Update: As plobsing has demonstrated, it *can* be done in one line ... but it's really, really, really difficult :-)

    Cheers,
    Rob

      Whats wrong with the following?

      my $pdl = random(100, 100) * 4 + 1;
        It's the same:
        The following binary perl operators act on piddles: + * - / ** > < <= >= == != << >> | & ^ ! % <=> these behave identically to their ordinary Perl equivalents, except th +ey act element-by-element on the whole piddle.
        ...but you probably already knew that. :)
Re: Random Values (1-5) for a PDL?
by plobsing (Friar) on Mar 06, 2009 at 00:57 UTC

    Think about it. Play around with it. Figure it out. Thats what perldl is for.

    Hint: You can manipulate a uniform distribution with addition and multiplication with constants to obtain other uniform distributions.