Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found