From owner-freebsd-questions@FreeBSD.ORG Thu Aug 28 02:10:57 2014 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 49902DEA for ; Thu, 28 Aug 2014 02:10:57 +0000 (UTC) Received: from mail-pa0-x229.google.com (mail-pa0-x229.google.com [IPv6:2607:f8b0:400e:c03::229]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 1D81A381A for ; Thu, 28 Aug 2014 02:10:57 +0000 (UTC) Received: by mail-pa0-f41.google.com with SMTP id lj1so473075pab.14 for ; Wed, 27 Aug 2014 19:10:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=E5JnxSD19GKBExq5N/C5e5u5Rfi1G/XzuTEAabWUpkY=; b=bUJLlEotSp2Ppdp0L27bpbNcWHqpYkWPu9e8q8jM3SaLBiFTqfhWIorkGxUGxGlDqb 47Xi5syIKgyLxOFV5R+IxLdkTwHG1h4K+MltIwJxV50q6w1FVTWp16Nk5VLyLlU0Ys50 2Az1ji4TendNNGEqPhhAZUFbIPKzojxW6xnjlJWqxP8J3Bv7f4dyUrQt6mH78P4y2zAi Q0zgybSXQpACrA3JdS6bvrGS/+QoGxc50d3zcxVjeBmVIIa39+bdpxDt0Cz+TYWorn0A QnjOoWca+HwU8TstruZi+ByTB7dZg4RFZ2Kd/YsHBokXfeuK6K4mVovQnAXFWH2m6px3 rdxQ== X-Received: by 10.67.30.75 with SMTP id kc11mr1091355pad.75.1409191856490; Wed, 27 Aug 2014 19:10:56 -0700 (PDT) Received: from [10.240.140.110] ([123.58.191.67]) by mx.google.com with ESMTPSA id zm5sm1774180pbb.76.2014.08.27.19.10.54 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 27 Aug 2014 19:10:55 -0700 (PDT) Content-Type: text/plain; charset="utf8"; Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) Subject: Re: Ask for opinion: changing rand(3) to random(3) in awk(1) X-Pgp-Agent: GPGMail (null) From: Chenguang Li In-Reply-To: <44mwapn1pw.fsf@lowell-desk.lan> Date: Thu, 28 Aug 2014 10:10:34 +0800 Content-Transfer-Encoding: 8bit Message-Id: References: <44mwapn1pw.fsf@lowell-desk.lan> To: freebsd-questions@freebsd.org X-Mailer: Apple Mail (2.1878.6) Cc: Lowell Gilbert X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Aug 2014 02:10:57 -0000 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 Lowell Gilbert 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-----