in reply to making random number

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

Replies are listed 'Best First'.
Re^2: making random number
by Anonymous Monk on Aug 25, 2014 at 06:12 UTC

    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

        thanks a lot Dave.i will come back with code. again, thank you.

        this is my first code,but it doesn't work. :-(

        #!/usr/bin/perl my $random_number = int(rand(100)); print 0912$random_number, "\n";