The rand function will return a value between 0 and less than $n when called like: my $number = rand($n). The int function can strip away the floating point portion, leaving you with an integer between 0 and $n-1. So if you want eight digits, you might do something like "my $number = int(rand(100_000_000));".

The next problem is you want some fixed digits to come first. You might try this:

my $number = sprintf "932%08d\n", rand(100_000_000);

sprintf can be used to format a string, with zero-padding if desired. And while you're at it, that's a good place to truncate the floating point portion, and to prepend the "932" digits.

And your last problem is you want 1000 of these numbers. For that you would probably use a while loop, or a foreach loop, or even map. The first two are discussed in perlsyn.

This is homework, as you've said. You need to read perlintro, perlsyn, rand, int, and sprintf to figure out the "sprintf" solution to your problem. I suggest you do read those documents.


Dave


In reply to Re^3: making random number by davido
in thread making random number by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.