Skip to content

Instantly share code, notes, and snippets.

@butlermatt
Created September 15, 2015 12:21
Show Gist options
  • Save butlermatt/eb3258f89c70e7ad116f to your computer and use it in GitHub Desktop.
Save butlermatt/eb3258f89c70e7ad116f to your computer and use it in GitHub Desktop.
Quick and dirty lottery draw
import 'dart:math';
void main() {
var tm = new TicketManager();
tm.buyTicket('WeyLand-Yutani', 50000);
tm.buyTicket('srolls', 10000);
tm.drawTicket();
}
class TicketManager {
num curTicket;
List<Ticket> tickets;
TicketManager() {
tickets = new List<Ticket>();
curTicket = 1;
}
void buyTicket(String name, num quanity) {
var tick = new Ticket(name, curTicket, curTicket + quanity);
curTicket += quanity;
tickets.add(tick);
}
void drawTicket() {
var rand = new Random();
var myNum = rand.nextInt(curTicket - 1);
myNum += 1;
print('Ticket: $myNum');
var winner = tickets.firstWhere((el) => el.hasTicket(myNum));
print('Winner: ${winner.name}');
}
}
class Ticket {
String name;
num start;
num end;
Ticket(this.name, this.start, this.end);
bool hasTicket(num winner) => start <= winner && winner <= end;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment