Re: making random number
by CountZero (Bishop) on Aug 25, 2014 at 06:37 UTC
|
It is not more difficult than this: use Modern::Perl qw/2014/;
say 91200000000 + int(rand(100000000)) for 1..1000;
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] [d/l] |
|
| [reply] |
|
use Modern::Perl qw/2014/;
open my $OUT, '>', './random.txt' or die "Could not open the output fi
+le: $!";
say $OUT 91200000000 + int( rand(100000000) ) for 1 .. 1000;
Now check the documentation for open, print and say. If there is anything you do not understand, come back here and ask.These are really basic functions you will need time and time again, so it is important you understand them completely.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] [d/l] [select] |
Re: making random number
by Ratazong (Monsignor) on Aug 25, 2014 at 06:08 UTC
|
See the example in rand().
To get an 11-digit number starting with 912, I propose to generate a random 8-digit-number and put the 912 in front.
To get many numbers, you need a loop, e.g. a for-loop or a while-loop. (search for it in perlsyn)
HTH, Rata
| [reply] |
Re: making random number
by davido (Cardinal) on Aug 25, 2014 at 06:10 UTC
|
You need 1000 eleven-digit random numbers that all start with 912? ....so you really need 1000 eight-digit numbers, and need to prepend the digits 912 to them? What have you tried so far? What problem are you solving?
| [reply] |
|
| [reply] |
|
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.
| [reply] [d/l] [select] |
|
|
|
|
|
Re: making random number
by karlgoethebier (Abbot) on Aug 25, 2014 at 08:03 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
open( URANDOM, "</dev/urandom" ) || die $!;
read( URANDOM, $_, 4 );
close URANDOM;
srand( unpack( "L", $_ ) );
my $x = 91_200_000_000;
my $y = 100_000_000;
print map { $x + int( rand($y) ) . qq(\n) } 1 .. 1000;
__END__
91268590986
91212775408
91220942039
91291708260
91205208740
91217762181
91242983060
91202706729
91288735095
...
See also open, read, close, srand, map (mentioned already above by davido), unpack as well as TMTOWTDI and /dev/random
Regards, Karl
«The Crux of the Biscuit is the Apostrophe»
| [reply] [d/l] |
|
| [reply] |
|
| [reply] |
|
| [reply] |
Re: making random number
by Anonymous Monk on Aug 25, 2014 at 21:11 UTC
|
Since it is h*o*m*e*w*o*r*k, the entire purpose of the exercise is for you to figure it out ... as so many others before you have dutifully done ... for yourself. | [reply] |