Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/simplescrambler.rb

Instance Method Summary collapse

Instance Method Details

#scramble(min = 10, max = 100) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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