Skip to content

Instantly share code, notes, and snippets.

@01010111
Last active November 9, 2018 21:57
Show Gist options
  • Select an option

  • Save 01010111/48701d2dc2c7f6768b2cfc7bd7fcabd8 to your computer and use it in GitHub Desktop.

Select an option

Save 01010111/48701d2dc2c7f6768b2cfc7bd7fcabd8 to your computer and use it in GitHub Desktop.
Haxe Lottery
class Lottery
{
var entries:Array<{ name:String, chance:Float }> = [];
var lots_total:Float = 0;
public function new() { }
public function add_entry(participant:String, chance:Float = 1)
{
entries.push({
name: participant,
chance: chance
});
lots_total += chance;
}
public function get_winner():String
{
if (lots_total <= 0) return null;
var n = Math.random() * lots_total * 1000000;
var current_lot:Float = 0;
for (entry in entries)
{
current_lot += entry.chance * 1000000;
if (current_lot >= n) return entry.name;
}
return '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment