No such thing as a small change | |
PerlMonks |
Re^3: making random numberby davido (Cardinal) |
on Aug 25, 2014 at 06:24 UTC ( [id://1098492]=note: 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:
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 Section
Seekers of Perl Wisdom
|
|