Your skill will accomplishwhat the force of many cannot PerlMonks

making random number

 on Aug 25, 2014 at 06:00 UTC Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi friends,i am beginner in Perl and i want to make random numbers say with 11 digits which begins for example with 912.could you please help me? for example 1000 number.

Replies are listed 'Best First'.
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

```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
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

Re: making random number
by davido (Archbishop) 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?

Dave

it is a homework!!i am sorry, but i dont know how to start.now, i am reading the rand page which has been send to me.

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

Re: making random number
by karlgoethebier (Prior) on Aug 25, 2014 at 08:03 UTC

I put in my two cents:

```#!/usr/bin/perl

use strict;
use warnings;

open( URANDOM, "</dev/urandom" ) || die \$!;
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
...

Regards, Karl

«The Crux of the Biscuit is the Apostrophe»

thank you Karl, could you say me, how can i show the result in a text file, please?

See Redirection and Input and Output.

Update:...and don't forget to show the solution you figured out... this is always of interest...

Regards, Karl

«The Crux of the Biscuit is the Apostrophe»

The web search engines are your friends ;-)

((\$_="Mzz ojjdloobnf jt uvy5502383")=~y~b-zg2-5c96-81~a-z0-9~s)=~s~~~s; print
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.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1098488]
Approved by Athanasius
Front-paged by toolic
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2018-01-22 17:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How did you see in the new year?

Results (235 votes). Check out past polls.

Notices?