dimanche 22 novembre 2015

Two randomly generated arrays are always the same, why?

I'm trying to generate two arrays with randomly generated content. Each time I run my code, the content does change, however the two arrays are always the same, and I want them to be different. The relevant code it as follows:

[@board1, @board2].each do |board|
  15.times { board.place_random_ship }
end

Then within my Board class I have these two methods:

def place_random_ship
  self.full? ? (raise puts "error") : self.populate_grid
end

def populate_grid
  position = [rand(@grid.length), rand(@grid.length)]
  self.empty?(position) ? self[position] = :s : self.populate_grid
end

I tried putting srand at the beginning of my populate_grid method, but that didn't do anything. I also made sure the boards I pass in are different. They have really similar IDs, but they are different:

#<Board:0x007fc8e58641f0>
#<Board:0x007fc8e58641a0>

How do I get my arrays to have different content?




Aucun commentaire:

Enregistrer un commentaire