From owner-freebsd-hackers@FreeBSD.ORG Thu Aug 28 09:52:04 2014 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 7D6E6287 for ; Thu, 28 Aug 2014 09:52:04 +0000 (UTC) Received: from mail-lb0-x230.google.com (mail-lb0-x230.google.com [IPv6:2a00:1450:4010:c04::230]) (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 04CF61B71 for ; Thu, 28 Aug 2014 09:52:03 +0000 (UTC) Received: by mail-lb0-f176.google.com with SMTP id s7so591254lbd.35 for ; Thu, 28 Aug 2014 02:52:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=IUo6KO4D0nNltf5t+2lFUdHjezC0zljQwVX/e2iDam8=; b=uyjOi1Ib453ZEzZcIROAUyR7ZQPRJb1HgeOnDefaNwwvV48xJ3gKeYy1ESB1uSTprs pHy2vsLXH1UwvDVJnziDIRDGvFTBGV8nhbn0K2Z92itydQvNKim/A3yS+0pB1A72FWIJ UKz3/MqX3UACumaJFAt4Hv8yYk+HZH4BcLIYnzbI89psSSBjntT/vdlne30ThzBcWTBs SGdT4ZeFhAcV/koyQ3L8ByLrx1pb13H+kmPqDuSh1oGTvmD1GdEja9Vth44aYnjr+5ls c8nHNiPOOSylIdxBfo+Ty75K+3uXL68SkmuEzwD0IW5kGhBHf+Z+SL17ioWyaj4fYoAS Pd1g== X-Received: by 10.152.45.101 with SMTP id l5mr3162489lam.20.1409219521995; Thu, 28 Aug 2014 02:52:01 -0700 (PDT) Received: from [172.29.2.131] (altimet-gw.cs2.dp.wnet.ua. [217.20.178.249]) by mx.google.com with ESMTPSA id xc7sm5151680lbb.21.2014.08.28.02.52.00 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 28 Aug 2014 02:52:01 -0700 (PDT) Message-ID: <53FEFBB8.5040305@gmail.com> Date: Thu, 28 Aug 2014 12:51:52 +0300 From: Vitaly Magerya User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:31.0) Gecko/20100101 Thunderbird/31.0 MIME-Version: 1.0 To: Chenguang Li , freebsd-hackers@freebsd.org Subject: Re: On changing rand(3) to random(3) in awk(1) References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Aug 2014 09:52:04 -0000 On 2014-08-28 09:21, Chenguang Li wrote: > Since the original rand(3) could not provide a fair, "one-shot" randomness in awk(1), > I am writing this to suggest that we change *rand(3) to *random(3), which only requires > modifying a few lines of code[1], but will result in better random number generation. > BTW, OSX & gawk already have this. Previous discussion can be found here[2]. > > What do you think? I think this is a useful change; in particular, srandom(3) seems to do a much better job of coping with sequential seeds than srand(3), which solves a big problem with using 'srand' in our awk. > Index: main.c > =================================================================== > --- main.c (revision 270740) > +++ main.c (working copy) > @@ -74,7 +74,7 @@ > signal(SIGFPE, fpecatch); > > srand_seed = 1; > - srand(srand_seed); > + srandom(srand_seed); > > yyin = NULL; > symtab = makesymtab(NSYMTAB/NSYMTAB); > Index: run.c > =================================================================== > --- run.c (revision 270740) > +++ run.c (working copy) > @@ -1522,7 +1522,7 @@ > break; > case FRAND: > /* in principle, rand() returns something in 0..RAND_MAX */ > - u = (Awkfloat) (rand() % RAND_MAX) / RAND_MAX; > + u = (Awkfloat) (random() % RAND_MAX) / RAND_MAX; You should not use RAND_MAX with random(3), since it returns values between 0 and 0x7fffffff (inclusive); RAND_MAX only applies to rand(3). A better patch would be something like this: > - /* in principle, rand() returns something in 0..RAND_MAX */ > - u = (Awkfloat) (rand() % RAND_MAX) / RAND_MAX; > + /* random() returns values in [0, 2147483647] */ > + u = (Awkfloat) random() / 2147483648; Also, awk(1) man page should be updated; it currently says: > rand random number on (0,1) ... while it should say: > rand random number on [0,1) > break; > case FSRAND: > if (isrec(x)) /* no argument provided */ > @@ -1530,7 +1530,7 @@ > else > u = getfval(x); > tmp = u; > - srand((unsigned int) u); > + srandom((unsigned int) u); You should probably use 'unsigned long' here. > u = srand_seed; > srand_seed = tmp; > break; Otherwise, the patch looks fine.