Is there a more efficient way to generate a random file in Perl?


This is my first Perl script. Ever:


if ($#ARGV < 1) { die("usage: <size_in_bytes> <file_name>\n"); }

open(FILE,">" . $ARGV[0]) or die "Can't open file for writing\n";

# you can control the range of characters here
my $minimum = 32;
my $range = 96;

for ($i=0; $i< $ARGV[1]; $i++) {
    print FILE chr(int(rand($range)) + $minimum);


Its purpose is to generate a file in a specified size filled with random characters.

It works but it is pretty slow. It takes a few seconds to write a 10MB random file.
Does anyone have suggestions/tips on how to make it faster/better? Also feel free to point out common newbie mistakes.


If you need random numbers from a range, I'm not aware of more efficient way. Your script adjusted to my likings:


use warnings;
use strict;

die("usage: $0 

Write stream data from /dev/random.

use File::Copy;
if ($#ARGV 
By : Incognito

This video can help you solving your question :)
By: admin