Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

making random number

by Anonymous Monk
on Aug 25, 2014 at 06:00 UTC ( [id://1098488]=perlquestion: print w/replies, xml ) 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

      thank you for your suggestion. but how can i print the result in a text file.please help me.

        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 (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?


    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 (Abbot) 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 $!; 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»

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1098488]
Approved by Athanasius
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found