Created
January 17, 2015 16:24
-
-
Save stuartbreckenridge/967783b7d60ea28e0e75 to your computer and use it in GitHub Desktop.
Number Generator for Lottery Games.
This file contains 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 NumberGenerator | |
{ | |
class func generateLottoNumbers(#count:Int,maxRange:UInt32, duplicatesAllowed:Bool = false) -> ([Int]) | |
{ | |
var i = 0 | |
var uniqueSet = NSMutableSet(capacity: count) | |
var numberArray1 = [Int]() | |
do{ | |
var genNumber = (Int(arc4random() % maxRange + 1)) | |
uniqueSet.addObject(genNumber) | |
numberArray1.append(genNumber) | |
++i | |
} while i < count | |
if duplicatesAllowed == false | |
{ | |
if uniqueSet.count != count | |
{ | |
return self.generateLottoNumbers(count: count, maxRange: maxRange) | |
} | |
} | |
numberArray1.sort({$0<$1}) | |
return numberArray1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment