Friday, April 24, 2009

Generate Power Ball numbers




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

class Lottery
attr_reader :lotteryArray, :maxNumber, :powerBall, :maxPB
#attr_writer :maxNumber

def initialize(arraySize, maxNumber, powerBall, maxPB)
@lotteryArray = Array.new(arraySize)
@maxNumber = maxNumber
@powerBall = powerBall
@maxPB = maxPB
end

def getUniqueLottery
loop do
checkDuplicate = 0
numRand = getRandomNumber(@maxNumber)

for i in 0...@lotteryArray.length
if numRand == @lotteryArray[i] || numRand == 0
checkDuplicate = 1
break
end
end

if checkDuplicate == 0
return numRand
end

end # end of loop
end # getUniqueLottery

def buildLottery
for i in 0...@lotteryArray.length
@lotteryArray[i] = getUniqueLottery
end
end # buildLottery

def sortLottery
return @lotteryArray.sort
end

def printLottery
a = sortLottery
a.each do |num|
printf "%2s ", num
end

if @powerBall == "Y"
printf "\tPB: %2s \n", getPowerBall
end
end # printLottery

def getRandomNumber(number)
randNum = rand(number)
end # getRandomNumber

def getPowerBall
randNum = 0
until randNum > 0
randNum = getRandomNumber(@maxPB)
end
return randNum
end # getPowerBall
end

# To run program
#
# ruby -w lottery.rb 5
#
# where TotalTickets is number of tickets that generates

# start program
lotto = Lottery.new(5, 56, "Y", 43)

#for i in 1..$TotalTickets.to_i
for i in 0...ARGV[0].to_i
lotto.buildLottery
lotto.printLottery
end

No comments:

Post a Comment