in reply to Re^2: making random number
in thread making random number

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

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

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

Re^4: making random number
by Anonymous Monk on Aug 25, 2014 at 07:29 UTC

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

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

      Indeed not. When compiling you will doubtless have seen these error messages:

      Scalar found where operator expected at 1098497.pl line 3, near "0912$ +random_number" (Missing operator before $random_number?) Illegal octal digit '9' at 1098497.pl line 3, at end of line syntax error at 1098497.pl line 3, near "0912$random_number"

      which pretty much describe and define your problem. Have you thought at all about how you might tackle these? Have you read perlintro and perlop yet?

      print "0912$random_number\n";

      This works (look at the small difference to your solution). However if the random-number is less than 10, it will give you a 5-digit-number instead of a 6-digit-number. That issue may be circumvented by the approach shown by CountZero below (adding 91200 to your random number).

      I wouldn't expect that to work. You needed an 8-digit random number with 912 prepended to it. Your code will produce a 2-digit random number, and then line 3 won't even compile. Even if it did compile, it would fail to produce an 8-digit (with three or four digits prepended) anytime the random number has leading zeros. That's why I used 'sprintf'.

      CountZero was correct that just adding 91200000000 to any eight digit (or less) random number will give you the correct number of digits. But now you seem to be putting a zero at the beginning too, so in that case you probably need a string-based approach, which is what sprintf will do for you.

      Are you generating phone numbers within an area code? Is it really seven-digits you're after? :)

      Where are the text book, professor, teacher's aid, and other classmates through this process? This assignment certainly didn't appear without prior explanation. You may find the learning process, at your early stage, to be more productive if you could spend 10 minutes face to face with an instructor or teacher's aid.


      Dave