Shopximity/jlib — pull request #1 by EchNet (2013-04-28 03:22:36+00:00)
Title: Weighted choice

Created this repository for Swoop general purpose Java utilities.

This branch / pull request includes a weighted choice randomization framework and reference mini-app!

Weighted choice implementation takes total weight and does a weighted random selection in a single pass over the choice array. See com.swoop.util.Randomizer and com.swoop.util.RandomizerTest (unit test). TODO: select N items in a single pass - it's complicated but doable by extension of the current approach. Fallback is to select N items in N passes, eliminating the selected items by dropping their weights to zero.

Weighted choice reference app composes mythic sentences out of random words and random patterns. As each word/pattern is used, its weight is decreased, decreasing the chances that it will be selected again. See the classes in package com.swoop.example. To run the program, execute the shellscript jlib/run.