Saturday, August 17, 2013

random number generator gives same number in array twice for a given game (bingo)

random number generator gives same number in array twice for a given game
(bingo)

I am trying to create a bingo game, and I have a board that gets random
generated numbers on it for each user. However, for every user it
generates the same random board. Effectively @places, gets put in array
@bingo_cards. But instead of being two different hashes of numbers, they
end up the same. There is something wrong with my method below.
The first method sets up the board, and the second method picks numbers
for it
def start_game(user_goes_first)
#bingo slots
@places = {
a1:" ",a2:" ",a3:" ", a4:" ", a5:" ",
b1:" ",b2:" ",b3:" ", b4:" ", b5:" ",
c1:" ",c2:" ",c3:" ", c4:" ", c5:" ",
d1:" ",d2:" ",d3:" ", d4:" ", d5:" ",
e1:" ",e2:" ",e3:" ", e4:" ", e5:" "
}
@places_keys = [
:a1,:a2,:a3,:a4,:a5,
:b1,:b2,:b3,:b4,:b5,
:c1,:c2,:c3,:c4,:c5,
:d1,:d2,:d3,:d4,:d5,
:e1,:e2,:e3,:e4,:e5
]
@bingo_cards = []
@user_name.each do |numbers|
@places_keys.each_with_index do |n,i|
@places[n] = pick_number(i)
end
@bingo_cards << @places
end
user_turn
end
def pick_number(num)
#generates random numbers that make up the bingo board(s)
case num
when 0..5
rand(1..15)
when 6..10
rand(16..30)
when 11..12
rand(16..30)
when 13
"X"
when 14..15
rand(16..30)
when 16..20
rand(31..45)
when 21..25
rand(46..60)
else
0
end
end

No comments:

Post a Comment