Here's the awk script:
# Load in comments file and seed random number generator
BEGIN {
cmt_file = "/home/dir/comments.txt"
while (getline cmt[i++] < cmt_file)
gsub(/ /, "<SP>", cmt[i-1]);
# seed random number generator via shell random number
# Why? Because sted.awk is run 3 times in a row so fast that the time
# used for srand as srand() makes the same random number each time
since
# srand uses whole seconds.
"echo $RANDOM" | getline random;
srand(random);
(! GRADE) && GRADE=5;
}
.
.
.
# Add CONTENT line for random comments
/CONTENT=/ {
# try taking every 4th random number to get around some lines having
# the same comment twice in a row despite random generation.
for (i=0; i<3; i++) rand();
comment = cmt[int(rand() * 30)];
match($0, /.*ctl[0-9]*_3/);
nline = substr($0, 1, RLENGTH - 1) "6" substr($0, RLENGTH + 1);
sub(/CONTENT=.*/, "CONTENT=" comment, nline);
print nline;
}
Thanks.
When last I ran into this problem, what I did was to save the last value
returned by rand() to a file, then on the next run, read that in and use
that value as the arg to srand(). Worked well.
Note: You may have to multiply the value by something like 32000 since
rand() returns a small decimal and srand() takes an integer. Anyway,
you get the idea...
You could so something like add PROCINFO["pid"] to the value of the time,
or use that as the seed.
>When last I ran into this problem, what I did was to save the last value
>returned by rand() to a file, then on the next run, read that in and use
>that value as the arg to srand(). Worked well.
>
>Note: You may have to multiply the value by something like 32000 since
>rand() returns a small decimal and srand() takes an integer. Anyway,
>you get the idea...
Gawk doesn't use rand() internally, so it should be producing a larger
range of values.
--
Aharon (Arnold) Robbins arnold AT skeeve DOT com
P.O. Box 354 Home Phone: +972 8 979-0381
Nof Ayalon Cell Phone: +972 50 729-7545
D.N. Shimshon 99785 ISRAEL
Is that good enough (random enough) for your task?
BEGIN {
"od -tu4 -N4 -A n /dev/random" | getline
srand(0+$0)
}
BR,
Thomas
I wasn't implying that it was in any way limited. Rather, I was saying
that it returns a value in the 0..1 range, and that you might want to
remap that to something in the 0..(some large integer) range.
Note that "man gawk" is somewhat terse about what sort of values should
be used as the argument to srand(), but it does mention the time of day,
which implies something like a 32 bit integer.