mardi 10 janvier 2017

Get a random non repeating element of an array in ruby

I have an array of questions and I want to get a non-reapting random one out of them. So for instance 5 questions, and if asked all i will simply start again. I would like to put it into an method (or something like that)

def askrandom
questions = ["A?", "B?" , "C?" , "D?"]

return #random question
end

The output should be something like

A? C? D? B? #all questions where asked once, so repeat

B? D? C? A? ...




Aucun commentaire:

Enregistrer un commentaire