Ask for opinion: changing rand(3) to random(3) in awk(1)

Chenguang Li horus.li at gmail.com
Thu Aug 28 02:10:57 UTC 2014


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Lowell Gilbert <freebsd-questions-local at be-well.ilk.org> wrote:

I had thought about that a few years ago, but I didn't have any cases
where it mattered. Note that your example (footnote 1) is *not* such a
case. The bug in that case is calling srand() multiple times, rather
than just once at the beginning. 

You could take a patch to the security or hackers list, but first you
should figure out what problem you are trying to solve. 

Oops, I think you're right, I just gave the post a glance, should have read it through and described my issue as "the same symptom".

The problem I was trying to describe was its "one-shot" randomness, take these two as examples (where it matters):

1. You wrote a script[1] that simulate rolling a dice, it would produce the same result if executed within, say, 5 seconds.
2. You have a CGI script which will show different content based on the number generated by rand().

In the first situation, you can generate all the outcomes in a single run by using for-loop, but the first outcome will be the same. OSX's awk(1) will produce a reasonable number every time I run it. In the latter one, you could call rand() once and throw away the result, and call it again to get another number. Both are practical workarounds, but we do have a better choice: applying the modification I suggested before.

If others are not affected by the problem I described above, then I am okay with that. The other reason why I suggest this is, I see no loss, only to make it better.

[1] BEGIN { srand(); print int(1+rand()*6); } or BEGIN { srand(); } { print int(1+rand()*6); }, won't matter.

- ---
Chenguang Li

-----BEGIN PGP SIGNATURE-----

iQIcBAEBCgAGBQJT/o+jAAoJELG4cS+11lRhYWkP/317ZhT5pJ+2Gv/nTqRdqVJk
As8E2v5EKU8sSn3A/K+B2ME0LJKBz1gJYzjemu63KxJvsayQb2EEeNz4cJOY41MI
8UskESrkJlQVEcB7snO3mSN78UEfSYNksDt1v7fXRY+1WKcZboLn5b7jrPEzqtV6
8rxR4lgvxTHNK7xvDdmuUDlVW4mBt6fZPbCVjpok0TGXS3+iJb6L4KoOZJ9m7Yz0
/svVqBkYEZIsVrehS+lc2cTW+vrkh6aVeFytOCcNFEtN7bTF4t32d4e/SYahMBiA
TKih1Ta1DnMmP5z9MVbFV9jgm/5e8djZmzZlAFSTrggIQsZ9P0iqSX07z3hwcLUZ
hj5R/ir6tugGao/kc5fmaYHTCIIjTKhwn+jEftF/heLF/EZ3KOYybA8pxzKveBY2
xwZI26NDGULgrdV/XmWJt5hUPZ/yDBbsBY73YRaXS7MjIJF2JsA0UPh6AUIXd3nG
F9zDgdw/Zl48vnvY7mn90vG6QwdsJJ9vKLyP8Hj1UGLf5FgstkpLk1v6WuHkpsUl
Ka7DzbSuitp5uIakEllRwdN1UlPl90TSbNhjku/rDrudJ9eOVVEYUDqkXMohlclM
pPLgasruPpqvhgglDUTKRwJz9vRFpUiShPw5JbtsAQlkOwtAKg3mjs1rR2q9XhLL
3PQFyTisBAatJRpYXI0Q
=EIal
-----END PGP SIGNATURE-----


More information about the freebsd-questions mailing list