On 05/25/2018 09:19 PM, ToddAndMargo wrote:
Hi All,

     This is my keep file submitted for your critique.

Many thanks,
-T


Perl 6: random numbers:

`rand` return a positive real number, always less than one:
     $ p6 'say rand;'
     0.268091668368972


Place a number in front of it to get a larger number:

     $ p6 'say 1000.rand;'
     565.2817971799526


To convert to an positive integer, use truncate:
     $ p6 'say 1000.rand.truncate;'
     876

Follow up:

This is my latest keeper on random numbers.

In my program were I use this, I wanted a random positive four
digit integer, so I used:
        my $BaseName   = (1000..9999).pick;

Thank you all for the help with this!

-T


Perl 6: random numbers:


"pick" a number between a range:
    $ p6 'say (^1000).pick'
    485

  Note: `(^1000)` is shorthand for (0..1000) excluding the last number

    p6 'say (4..9).pick'
    7

    $  p6 'say (1000..9999).pick'
    7024



`rand` return a positive real number, always less than one but
greater than zero:

    $ p6 'say rand;'
    0.268091668368972


Place a number in front of it to get a larger number:

    $ p6 'say 1000.rand;'
    565.2817971799526

Between 4 and 8:

    $ p6 'say (4..8).rand;'
    6.168408720956085



To convert to an positive integer, use truncate:
    $ p6 'say 1000.rand.truncate;'
    876

A random real number to two places:
    $ p6 'say 1000.rand.truncate/100.0;'
    1.49

Reply via email to