Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: quick & easy random dir vector, how?
Date: Wed, 05 Jun 2019 08:05:49 -0700
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <86d0jsf4xe.fsf@mailhub.linuxsc.com>
References: <8b69f6c2-5be5-4f24-9070-86dd24a42cd5@googlegroups.com> <24945ada-c036-4b67-91a3-714f0ff5e5ef@googlegroups.com> <87r2891i5d.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="4555b590f13cc0f9d9b6c6288edfc914"; logging-data="26962"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+wDTDmDcfMfL7HSQV+/VMG+dL/YZKL1Zc="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:oSuarbSljRTKkmr6hlmWCPJgbcM= sha1:OQ7eWgTb21939WfX84l6lbM6NgQ=
Xref: csiph.com comp.lang.c:144484
richard@cogsci.ed.ac.uk (Richard Tobin) writes:
> In article <87r2891i5d.fsf@bsb.me.uk>,
> Ben Bacarisse wrote:
>
>>> I will say this with regard to my solution... as I see it, I'm
>>> trying to get random values on the surface of a sphere.
>>
>> That's easy. Pick x, y and z in [-1, 1] and normalise. You don't get a
>> uniform distribution though.
>
> Pick x, y and z in [-1, 1]. If they're outside the unit sphere, try
> again until they are inside - this will take 2 x 3 / pi attempts
> average. You now have a point with uniform distribution within the
> unit sphere. Normalize to get a point on the surface.
Slight refinement, assuming a 64-bit integer RNG is used: generate
one 64-bit random number, divide into three 21-bit values mapped to
the range [-1048575,1048575] (discarding samples that would produce
the value -1048576), testing x*x + y*y + z*z <= 1048575*1048575.
When the test passes normalize appropriately.
> Apologies if this has already been suggested - I haven't read the
> whole thread. And it's not particularly efficient.
But probably more efficient than a "more direct" method using trig
functions, especially since the rejection tests can be done using
integer-only calculations.