dimanche 2 février 2020

Test that a method does not select a certain element at random

I have the following method

def select_random(mylist, excluding_this_one)
   return mylist.sample
end

Currently it is broken. There is a chance that given the input [5,8,6], 8 that it would select option 2. I would like it to only select an element at random which isn't 8 (so 6 or 5).

I'm currently trying to write a test to ensure this happens. But given that the output of select_random is not deterministic, I have no way to test that it will never return 8.

My question is, what can I do to ensure that my method select_random will never return 8?

Aucun commentaire:

Enregistrer un commentaire