http://www.perlmonks.org?node_id=1007396

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

I am trying generate 100 random numbers using forach

between 1 thru 99

But always get between 0 and 1. Any ideas?

perl -wle "use 5.10.0; say rand foreach(1..100) ;"

Thanks.

Replies are listed 'Best First'.
Re: Random generator fail
by MidLifeXis (Monsignor) on Dec 05, 2012 at 21:10 UTC

    See the documentation for rand. It explains pretty well why this is happening.

    --MidLifeXis

Re: Random generator fail
by blue_cowdawg (Monsignor) on Dec 05, 2012 at 21:18 UTC
        But always get between 0 and 1. Any ideas?

    Works as designed.

    $ perl -e 'use 5.10.0; say int(6*rand())+1 foreach(1..10);' 6 5 4 6 2 2 4 6 3 5
    Is that what you were after? Or at least something like it?

    As MidLifeXis pointed out:

    rand EXPR rand Returns a random fractional number greater than or equa +l to 0 and less than the value of EXPR. (EXPR should be posit +ive.) If EXPR is omitted, the value 1 is used. Currently EXP +R with the value 0 is also special-cased as 1 (this was undocu +mented before Perl 5.8.0 and is subject to change in future ve +rsions of Perl). Automatically calls "srand" unless "srand" h +as already been called. See also "srand". Apply "int()" to the value returned by "rand()" if you +want random integers instead of random fractional numbers. +For example, int(rand(10)) returns a random integer between 0 and 9, inclusive. (Note: If your rand function consistently returns numbe +rs that are too large or too small, then your version of Perl w +as probably compiled with the wrong number of RANDBITS.) "rand()" is not cryptographically secure. You should n +ot rely on it in security-sensitive situations. As of this wri +ting, a number of third-party CPAN modules offer random number generators intended by their authors to be cryptographi +cally secure, including: Math::Random::Secure, Math::Random::MT::Perl, and Math::TrulyRandom.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Random generator fail
by BillKSmith (Monsignor) on Dec 06, 2012 at 04:34 UTC

    Do you want real numbers in the range (1 <= number < 99) on a windows system?

    perl -e"print 1 + rand 98, qq(\n) for 1..100"
    Bill

      Ok I have better understanding now, Bill your solutions works great!

      @MidLifeXis What is rational for rand to ignore $_. Appearently you pointing out that, solves my problem by using

      perl -wle "use 5.10.0; foreach (1..100) { say rand($_);};" 1.94992065429688 56.7288208007813 52.1597290039063 18.6995849609375 6.6961669921875 68.6776733398438 65.6177978515625 62.72314453125 52.1324157714844 ...

      But I think using additional line  { say rand($_); is completely un-neccessary. Thank you.

        Let's step through this:

        foreach (1..100) { say rand($_); }
        When $_ is equal to ...
        • ... 1, rand($_) cannot be larger than 1.
        • ... 2, rand($_) cannot be larger than 2.
        • ... 3, rand($_) cannot be larger than 3.
        • ... 4, rand($_) cannot be larger than 4.
        • ... 5, rand($_) cannot be larger than 5.
        • ... 6, rand($_) cannot be larger than 6.

        Look at one of my earlier posts on this thread. The documentation for rand states that 0 <= rand($x) < $x if $x > 0. To accomplish what you are trying to do would require that you do this:

        say 1 + rand(99 - 1) for (1..100)
        In my previous node, MAX = 99 and MIN = 1. The 1..100 is the number of iterations you wish to use. If you use rand($_), your results will be biased toward the lower end of the counter range.

        --MidLifeXis

        My rand 98 generates random numbers in the range (0<= number < 98). Adding one to every random number changes the range to (1<=number<99). Using rand $_ would generate a very different sequence of numbers. In fact, the sample output that you show cannot be the first few numbers as your dots suggest. It is possible that they are the last few numbers.
        Bill

        forgot to add this:

        perl -wle "use 5.10.0; say int foreach (1..100) ;"

        Works as rand() should have.

Re: Random generator fail
by kcott (Archbishop) on Dec 07, 2012 at 09:03 UTC

    Here's my $0.02 worth:

    $ perl -E 'say 1 + int rand 99 for 1..3' 28 94 43

    Obviously, change the 3 to 100.

    Update: You appear to want decimals although you asked for a range using integers. Perhaps you could clarify.

    This gives: 1.0 < random_number < 99.0

    $ perl -E 'say 1 + rand 98 for 1..3' 38.6240726329874 72.0054041794683 94.0423383452302

    -- Ken