From owner-svn-src-all@FreeBSD.ORG Tue Jul 2 20:17:53 2013 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 7F760456 for ; Tue, 2 Jul 2013 20:17:53 +0000 (UTC) (envelope-from mailer-daemon@vniz.net) Received: from mail-la0-f53.google.com (mail-la0-f53.google.com [209.85.215.53]) by mx1.freebsd.org (Postfix) with ESMTP id 04A751321 for ; Tue, 2 Jul 2013 20:17:52 +0000 (UTC) Received: by mail-la0-f53.google.com with SMTP id fs12so6056903lab.12 for ; Tue, 02 Jul 2013 13:17:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:openpgp:content-type:x-gm-message-state; bh=F9Zxb2Zj0WO4Fd6B2ViF2FCgIPhfZYeJIxJAXPcdU7g=; b=g3iFbfmqPAsd2BDtbKRS6wMvkgXf8t7abM9u/qHdGa9wqVk3kqDdLRY0L9mJaYaMmn MSoJmNDYgCGNuWBWHeLN/cpx1TzfsJ9TyjUwaegrK0pAN6gt+h5UpdzvU3UOLzoncCpN e+KRvEpyW3h+dkD3/Ncd9VTn5DkixTtctXXf//zCQITe+FTcVuaqCougo9B3Cb+BTuqk 1rc69ADcR7S1mKd4AcIUANW8aNpY5UJW9l02CHSrYacgeZkprTUledkjXghsRWX9d4AO bIO80AS1l0ZNbRx8Va6t5zIakvOga9UoyTrCrT/GXnYw50HAXrwPep6kajQEm4tu+5jD msoA== X-Received: by 10.112.159.169 with SMTP id xd9mr14648379lbb.43.1372796271201; Tue, 02 Jul 2013 13:17:51 -0700 (PDT) Received: from [192.168.1.2] ([89.169.163.3]) by mx.google.com with ESMTPSA id p10sm9645739lap.8.2013.07.02.13.17.50 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 02 Jul 2013 13:17:50 -0700 (PDT) Message-ID: <51D33552.1030208@freebsd.org> Date: Wed, 03 Jul 2013 00:17:22 +0400 From: Andrey Chernov User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 MIME-Version: 1.0 To: Bruce Evans Subject: Re: RAND_MAX broken References: <201307012143.r61Lhemi067176@svn.freebsd.org> <20130702130818.V865@besplex.bde.org> <20130702165642.X1571@besplex.bde.org> <51D2F571.8050108@freebsd.org> <20130703020550.E8632@besplex.bde.org> In-Reply-To: <20130703020550.E8632@besplex.bde.org> OpenPGP: id=964474DD Content-Type: multipart/mixed; boundary="------------010001020809020802050200" X-Gm-Message-State: ALoCoQmleE6X3f2Zn6qy4DkqjqAdS1d+szZ7Jw0DzX/aBrKPWJlCkK0nfrnssr0Bs40ATrJKVp/z Cc: svn-src-head@freebsd.org, svn-src-all@freebsd.org, "Pedro F. Giffuni" , src-committers@freebsd.org X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 02 Jul 2013 20:17:53 -0000 This is a multi-part message in MIME format. --------------010001020809020802050200 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit On 02.07.2013 20:33, Bruce Evans wrote: > I checked the values returned by rand(). The ACM part works as > intended, so it never returns RAND_MAX. It also never returns 0. So > the distribution of values in the documented range [0, RAND_MAX] is > very non-uniform. It is uniform in [1, RAND_MAX - 1]. To use this > algorithm for rand(), 1 should have been subtracted, giving a range > of [0, 0x7ffffffe]. Do you mean [0, 0x7ffffffd] (assuming 1 subtracted)? See attached patch. I don't see compatibility problems at least from POSIX specs point of view - they don't say something specific about RAND_MAX. -- http://ache.vniz.net/ bitcoin:13fGiNutKNHcVSsgtGQ7bQ5kgUKgEQHn7N --------------010001020809020802050200 Content-Type: text/plain; charset=windows-1251; name="patch.txt" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="patch.txt" --- stdlib.h.bak 2013-07-03 00:08:25.000000000 +0400 +++ stdlib.h 2013-07-03 00:10:17.000000000 +0400 @@ -69,7 +69,7 @@ #define EXIT_FAILURE 1 #define EXIT_SUCCESS 0 -#define RAND_MAX 0x7fffffff +#define RAND_MAX 0x7ffffffd __BEGIN_DECLS #ifdef _XLOCALE_H_ --- rand.c.bak 2013-07-03 00:08:00.000000000 +0400 +++ rand.c 2013-07-03 00:11:33.000000000 +0400 @@ -75,7 +75,8 @@ x = 16807 * lo - 2836 * hi; if (x < 0) x += 0x7fffffff; - return ((*ctx = x) % ((u_long)RAND_MAX + 1)); + *ctx = x; + return (x - 1); #endif /* !USE_WEAK_SEEDING */ } --------------010001020809020802050200--