Last active
November 9, 2018 21:57
-
-
Save 01010111/48701d2dc2c7f6768b2cfc7bd7fcabd8 to your computer and use it in GitHub Desktop.
Haxe Lottery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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