alan has asked for the wisdom of the Perl Monks concerning the following question:

I was wondering if anyone had a good technique for generating large (8 - 16K) blocks of random data for use in a test script aimed at driving system load... any hints? thanks

Replies are listed 'Best First'.
RE: generating random blocks of data...
by vroom (His Eminence) on Feb 25, 2000 at 01:55 UTC
    #!/usr/bin/perl my $size=int(rand(8192)+8192); my @array=(a..z,A..Z,0..9); while($size--){ print $array[rand(@array)]; }
    This will do it... it's pretty random just prints out anywhere from 8 to 16k worth of random characters... from @array. If you have any further constraints for the type of data let us know.

    Tim Vroom | vroom | vroom@cs.hope.edu

      One important thing left out here is to make sure that you use srand. If you don't, the block will be the same every time you run the program.

        Actually, perl is smarter than that. rand will call srand for you if you don't do it.

        michael
        the blue haired monk

      Just one small change, quotes around the letters.
      my @array=('a'..'z','A'..'Z',0..9);


      -- "A Jedi uses the Force for knowledge and defense, never for attack."
Re: generating random blocks of data...
by AidanLee (Chaplain) on May 08, 2001 at 00:12 UTC

    If you'd like any characters without specifying a character set to draw from:

    my @values = (); my $length = int( rand 8000 ) + 8000; push @values, int( rand 256 ) while( @values < length ); $random_stuff = pack "c$length", @values;
Re: generating random blocks of data...
by plaid (Chaplain) on Feb 25, 2000 at 02:12 UTC
    If you want some random binary junk, you might try opening up /dev/urandom and reading however many bytes from that. Unfortunately, I believe /dev/urandom is a Linux-specific device, and that's a pretty big limitation.
      thanks to (and the previous reply) both are great ideas and have me pointed in the right direction.. I'm acutally glad I got input from you guys on both random text and ideas for random binary junk.. :) /alan
Re: generating random blocks of data...
by Anonymous Monk on Feb 26, 2000 at 20:36 UTC
    It's important to understand that the above methods really generate psuedo random numbers. If you need strong (nondeterministic) random numbers, you need something like /dev/random and /dev/urandom. Short of this, you will need to obtain/create an entropy gathering process to handle this for you.
Re: generating random blocks of data...
by nickcave25 (Acolyte) on Feb 25, 2000 at 10:00 UTC
    I read somewhere recently (and, of course, I cannot now remember exactly where it was that I read it) that with Perl 5, you do not need to (and should not) use srand;. Does anyone else know the truth value of this assertion?
      In perl versions up to 5.003 (I think), srand needed to be called explicitly, but as of 5.004 (again, I think), srand was called implicitly with the first call to rand. Wnen srand is called implicitly however, the seed it is given is a number based on the time, which is predictable, so it's recommended to call it again with a better seed if it's for something more important, like cryptography purposes.
        The results from perldoc -f srand on the latest stable release of perl yield the following results. "In versions of Perl prior to 5.004 the default seed was just the current time(). This isn't a particularly good seed, so many old programs supply their own seed value (often time ^ $$ or time ^ ($$ + ($$ << 15))), but that isn't necessary any more."