Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.c Subject: Re: quick & easy random dir vector, how? Date: Fri, 31 May 2019 15:43:17 +0100 Organization: A noiseless patient Spider Lines: 66 Message-ID: <87ef4eit16.fsf@bsb.me.uk> References: <8b69f6c2-5be5-4f24-9070-86dd24a42cd5@googlegroups.com> <2cc42775-59ec-476e-a874-2a80f6360801@googlegroups.com> <87a7f3ig67.fsf@bsb.me.uk> <678a85fc-ee82-4a00-af6e-739b0432d7e4@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="080814d0aff56e2bbbb648b020bda0e0"; logging-data="13313"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/lVBuCPtP6iX/OTIrxzqaIFe3nHJPLdPE=" Cancel-Lock: sha1:WHfxM9QHzzgvm70olIH5B5hvS/c= sha1:CogRlemlYzFIbsj/V8Cwg8t0nkg= X-BSB-Auth: 1.d575dcc412bf6843187c.20190531154317BST.87ef4eit16.fsf@bsb.me.uk Xref: csiph.com comp.lang.c:144207 "Rick C. Hodgin" writes: > On Friday, May 31, 2019 at 8:55:04 AM UTC-4, Malcolm McLean wrote: >> On Friday, 31 May 2019 13:20:21 UTC+1, Rick C. Hodgin wrote: >> > On Thursday, May 30, 2019 at 9:08:57 PM UTC-4, Ben Bacarisse wrote: >> > > "Rick C. Hodgin" writes: >> > > > The discussion of good random algorithms came up a few months ago. >> > > > Chris Thomasson posted a great algorithm: >> > > > >> > > > https://groups.google.com/d/msg/comp.lang.c/RI0rdENv59s/BVUWgMBFDQAJ >> > > > >> > > > Use multi-channels, one for each [axis], with different seeds, and >> > > > then perform the math to determine your values between 0.0 and 1.0. >> > > >> > > I think it's unlikely that that is the problematic part. Once you have >> > > a source of uniformly distributed floating-point numbers, the question >> > > remains of how to pick a point on the unit sphere so that every point >> > > (AKA direction) is equally likely. >> > >> > If he's looking for random vectors, you would not seek to move on >> > the unit sphere, but in whatever directions there are on each axis. >> > >> > One could calculate the distance from your starting point to the >> > ending point so that you move one logical unit. An arbitrary velo- >> > city on each axis (within reason), and an arbitrary direction on >> > each axis, and you can move 1 unit at a time using completely ran- >> > dom values without worrying anything about the unit sphere. You >> > could calculate them as random values 0..2pi and then resolve their >> > (axis-relative) cos,sin coordinates to get real vector values, but >> > that's just one way to do it. >> > >> > Is mine a correct thinking? >> > >> No, you want each point on the sphere surrounding your origin to have >> an equal chance of being hit. >> So if you take a random point of longitude, you get a random point on >> the equator, which is a good start. But if you then take a random point >> of latitude, the points cluster up near the poles - not so good. >> >> Another approach might be to take random x, y, z on a uniform box. >> However then you get more random points towards the corners of the box. >> You can get round this be excluding all points that don't lie within >> the unit sphere. That's a bit icky, but it will work and is your first >> method. > > If you're using vectors, you have an arbitrary X, arbitrary Y, and > arbitrary Z direction you're moving. It should not bunch up on any > axis, and should be linear across the spectrum of random values. > > A random value for each axis computed between 0.0 and 2.0, then sub- > tract 1.0 to make it -1.0 to 1.0, and then scale by your velocity > vector whatever that is (a random value, or a known variable). The question was rather specific -- to pick a random direction. The solution in 2D is to pick a random point on the unit circle -- essentially just scaling [0, 1) to the range [0, 2pi). Try picking an x and y value and using the resulting direction. You will find they are not uniformly distributed. > I may be thinking incorrectly, but I don't think I am. :-) That's often a problem. We have that trouble. With maths, there is at least way to find out. -- Ben.