Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.utanet.at!newscore.univie.ac.at!aconews-feed.univie.ac.at!aconews.univie.ac.at!not-for-mail Newsgroups: comp.lang.java.programmer From: Andreas Leitgeb Subject: Re: Using Enumerated Types as Array Indexes References: <4e4b0d81$0$314$14726298@news.sunsite.dk> <4e4b1fe4$0$314$14726298@news.sunsite.dk> <6d418cda-dab3-43df-a9ec-293b43f2bbd8@glegroupsg2000goo.googlegroups.com> <9b2aglFuj7U1@mid.individual.net> <9b72b5FrgfU1@mid.individual.net> Reply-To: avl@logic.at User-Agent: slrn/pre0.9.9-111 (Linux) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: Date: 19 Aug 2011 20:05:52 GMT Lines: 14 NNTP-Posting-Host: gamma.logic.tuwien.ac.at X-Trace: 1313784352 tunews.univie.ac.at 71616 128.130.175.3 X-Complaints-To: abuse@tuwien.ac.at Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7253 Robert Klemme wrote: > All solutions to fix remainder into modulo but keep the wrap around > which have been proposed so far share a common disadvantage: if the > number range is not a multiple of the divisor the value distribution > will not be uniform. > > The solution I used to fix this employed a do while loop and > compareAndSet() to reset the value to the beginning of the range (0 in > this case) in a thread safe manner. There's a much simpler "meta-way" to fix it: just stick to powers of 2 for the array length - then the overflow *does* always happen at a multiple of the divisor and the value distribution uniform :-)