class Array

Public Instance Methods

scramble(min = 10, max = 100) click to toggle source
# File lib/simplescrambler.rb, line 84
def scramble(min = 10, max = 100)
        if max.class != Integer || min.class != Integer
                raise NotNumber
        elsif self.length == 1
                raise CannotScrambleArrayElement
        elsif self.count(self[0]) == self.length
                raise CannotScrambleArraySame
        elsif min > 0 && max >= min
                temp = self
                (min + rand(max - min + 1)).times do
                        random = rand(temp.length)
                        random2 = rand(temp.length)
                        if random2 == random
                                until random2 != random
                                        random2 = rand(temp.length)
                                end
                        end
                        onepos = random
                        random = temp[random]
                        twopos = random2
                        random2 = temp[random2]
                        temp[onepos] = random2
                        temp[twopos] = random
                end
                return temp
        elsif min <= 0
                raise TooSmall
        elsif max < min
                raise MinMaxMismatch
        end
end