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 | [reply] [d/l] |
|
|
| [reply] |
|
|
| [reply] |
|
|
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."
| [reply] [d/l] |
Re: generating random blocks of data...
by AidanLee (Chaplain) on May 08, 2001 at 00:12 UTC
|
my @values = ();
my $length = int( rand 8000 ) + 8000;
push @values, int( rand 256 ) while( @values < length );
$random_stuff = pack "c$length", @values;
| [reply] [d/l] |
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. | [reply] |
|
|
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
| [reply] |
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.
| [reply] |
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? | [reply] [d/l] |
|
|
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.
| [reply] |
|
|
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."
| [reply] |