Gustavo
Gustavo

Reputation: 311

Same random number ruby

Well i'm a ruby newbie and im trying to learn with RubyKoans but i got stucked with this test

def test_dice_values_should_change_between_rolls
 48     dice = DiceSet.new
 49     dice.roll(5)
 50     first_time = dice.values
 51    
 52     dice.roll(5)
 53     second_time = dice.values
 54     
 55     assert_not_equal first_time, second_time,
 56       "Two rolls should not be equal"
 57   end

and this is DiceSet class

5  class DiceSet
  6    attr_accessor :values
  7 ··
  8    def initialize
  9      @values = []
 10    end
 11 
 12    def roll(times)
 13      @values.clear
 14      times.times do |x|
 15        @values << ( 1 + rand(6))
 16      end
 17     end
 18 ····
 19    end

the thing here is that whenever i run the code it always generates the exact same set of numbers, this is the Output.

Two rolls should not be equal.  <[3, 2, 4, 1, 3]> expected to be != to  <[3, 2, 4, 1, 3]>.

in the test im calling DiceSet.roll two times and for those two times i get the exact same set of 'random' numbers when they're supossed to be diferent right? I figured that i just might create another instance of DiceSet in order to pass the test but im guessing that is not the objective of the test

Upvotes: 1

Views: 1240

Answers (4)

Evmorov
Evmorov

Reputation: 1219

It's better to use attr_reader instead of attr_accessor (like Ilya Tsuryev suggested). Because you don't want that client code cheats the dices. And it's more readable to use rand(1..6).

class DiceSet
  attr_reader :values

  def roll(set_size)
    @values = []
    set_size.times { @values.push rand(1..6) }
  end
end

Upvotes: 0

Saurabh Hirani
Saurabh Hirani

Reputation: 1248

Using a new array every time will solve the reference problem that dominikh pointed, but as he correctly said that will not guarantee you that 2 consecutive rolls have a different set of nos. In my implementation I remember the last throw and loop around till I get a different set:

class DiceSet
  attr_reader :values, :lastroll
  def initialize
      @values = []
      @lastroll = []
  end
  def roll(n)
      while @values == @lastroll
          @values = Array.new(n) { |i| i = rand(6) + 1 }
      end
      @lastroll = @values
  end
end

Upvotes: -1

Ilya Tsuryev
Ilya Tsuryev

Reputation: 2846

The following should work for this test:

class DiceSet
  attr_accessor :values

  def roll (times)
    @values = []
    times.times do |x|
      @values << ( 1 + rand(6) )
    end
  end
end

So we are creating new array for each roll.

Upvotes: 1

Dominik Honnef
Dominik Honnef

Reputation: 18430

The problem is that DiceSet#values returns a reference to an array, and that array stays the same for the whole lifetime of your DiceSet object. In DiceSet#roll you clear that array and then add new numbers. Since both calls to DiceSet#values return the same reference, the result of the first roll will be lost, and your test is comparing the array with itself.

I am not familiar with the RubyKoans and what requirements they have, i.e. if you DiceSet is supposed to store the values etc. If it is, then the most straightforward solution is to either use two DiceSets or use Object#dup to store a copy of the returned object for the test.

Be aware, however, that your test is fragile even with correctly functioning code, as there always is the chance that two consecutive rolls will return the exact same numbers. In this particular case it is relatively small but still very much existent.

Upvotes: 6

Related Questions