Skip to content

Instantly share code, notes, and snippets.

@davenotik
Last active January 13, 2016 04:10
Show Gist options
  • Save davenotik/beda9608c199ea170046 to your computer and use it in GitHub Desktop.
Save davenotik/beda9608c199ea170046 to your computer and use it in GitHub Desktop.
/// A stupid little Powerball number generator.
/// http://www.powerball.com/powerball/pb_howtoplay.asp
import 'dart:math';
void main() {
int timesToPlay = 20;
Powerball.playMultiple(timesToPlay);
}
class Powerball {
static Random rnd = new Random();
/// Play once.
static void play() {
List numbers = [];
int numbersNeeded = 5;
// Loop until we have 5 unique, random numbers 1-69.
while (numbers.length < numbersNeeded) {
// Pick a random number, shifted by 1 to ensure it's never 0.
int number = 1 + rnd.nextInt(69 - 1);
// Only add to our list of 5 numbers if it's not already there.
if (!numbers.contains(number)) numbers.add(number);
}
// Sort in consecutive order because order doesn't matter.
numbers.sort();
// Pick a random Powerball number, 1-26, shifted
// by 1 to ensure it's never 0.
int powerBall = 1 + rnd.nextInt(26 - 1);
print('(${numbers.join(') (')}) (($powerBall))');
}
/// Play multiple times.
static void playMultiple(int timesToPlay) {
for (var i = 0; i < timesToPlay; i++) Powerball.play();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment