top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Generate Random Numbers Using Perl

+2 votes
422 views

This is a Perl script to generate one hundred thousand (100,000) random numbers from 0 through 9 inclusively

 #!/usr/local/bin/perl

# Get the timestamp number.  This is the old method of generating an unqie random number seed.
 # my $time = time();

 # Get the process id number.  This is the new method of generating an unqie random number seed.
 my $process_id = $$;

 # Set the random number seed to the process id.
 srand($process_id);

 # Set how many random numbers (from 0 to 9) that you want to generate.
 my $max_numbers = 100000;

 # Open the output file to save to.
 open (FH, "> List_of_Random_Generated_Numbers.txt");

 # Get and display random numbers.
 for (my $loop=0 ; $loop < $max_numbers ; $loop++)
 {
    # Get random number from 0 to 10 exclusive.  Note that this will result with a real (decimal number).
    $random_number = rand (10);

    # Only get the first digit of the random number.
   $first_random_digit = $random_number % 10;

    # Print random digit to the file.
   print FH "$first_random_digit";
 }

 # Close output file.
 close FH;

 # Exit.
 exit 0;
posted Apr 6, 2014 by Amit Kumar Pandey

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button

...