Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 20 Dec 2004 17:47:54 -0800
From:      Brooks Davis <brooks@one-eyed-alien.net>
To:        "James R. Van Artsalen" <james@jrv.org>
Cc:        freebsd-amd64@freebsd.org
Subject:   Re: /usr/games/random bug?
Message-ID:  <20041221014754.GA13070@odin.ac.hmc.edu>
In-Reply-To: <41C778CD.1080602@jrv.org>
References:  <41C778CD.1080602@jrv.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--opJtzjQTFsWo+cga
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Mon, Dec 20, 2004 at 07:13:49PM -0600, James R. Van Artsalen wrote:
> 5-stable "/usr/games/random -e 99" seems to always return 0, which makes=
=20
> it very predictable.
>=20
> 5.2.1 does this too.  But i386 4.x seems to work as expected (an exit=20
> value between 0 and 98 inclusive).
>=20
> The source code line in question is
>=20
>              return (int)((denom * random()) / LONG_MAX);
>=20
> the assembler code is
>=20
>        .p2align 3
> .LC17:
>        .long   0
>        .long   1006632960
>=20
>        call    random
>        cvtsi2sdq       %rax, %xmm0
>        mulsd   24(%rsp), %xmm0
>        mulsd   .LC17(%rip), %xmm0
>        cvttsd2si       %xmm0, %eax
>        jmp     .L1
>=20
> I'm not familiar enough with IEEE fp rules or amd64 fp coding to say why=
=20
> this doesn't work, nor if this source code is even reasonable.

I believe the code is bogus.  The code says, compute a long
(denom*random) then divide it by the largest possiable value you can
store in a long.  Since integer math in C truncates, the value should
always be 0 unless denom*random() happens to be LONG_MAX in which case
it will be 1.

It's intresting that this code ever worked.  A quick check indicates
that this doesn not work on alpha or sparc64.  Apparently no one noticed
that this code has been broken for over a decade (we got it from UCB).
NetBSD switched from LONG_MAX to MAXRANDOM in 2000, but apparently
didn't actually read the code since it also has the bug.

-- Brooks

--=20
Any statement of the form "X is the one, true Y" is FALSE.
PGP fingerprint 655D 519C 26A7 82E7 2529  9BF0 5D8E 8BE9 F238 1AD4

--opJtzjQTFsWo+cga
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFBx4DJXY6L6fI4GtQRAiT5AKDH1hBsa6xruWC4p4a0I8qNq4/wtwCghcTW
OV5XfgZym8U0dIVJ3B6zbLQ=
=1ANS
-----END PGP SIGNATURE-----

--opJtzjQTFsWo+cga--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20041221014754.GA13070>