Module: Randomness

Defined in:
lib/main.rb

Overview

The main module, meant to be a namespace and not a mixin.

Instance Method Summary collapse

Instance Method Details

#randboolObject

Returns a random boolean value.



64
65
66
67
# File 'lib/main.rb', line 64

def randbool()
	return true if randint(1, 2) == 1
	return false
end

#randbools(amount) ⇒ Object

Returns the specified number of randomly chosen boolean values.



69
70
71
72
73
74
75
# File 'lib/main.rb', line 69

def randbools(amount)
	return_list = []
	for i in 1..amount
		return_list << randbool()
	end
	return return_list
end

#randchar(string) ⇒ Object

Returns a random character from a string.



48
49
50
# File 'lib/main.rb', line 48

def randchar(string)
	return randchoice(string.split(''))
end

#randchars(string, amount) ⇒ Object

Returns the specified amount of randomly chosen characters from the string.



52
53
54
# File 'lib/main.rb', line 52

def randchars(string, amount)
	return randchoices(string.split(''), amount)
end

#randchoice(list) ⇒ Object

Returns a random selection from the list.



18
19
20
# File 'lib/main.rb', line 18

def randchoice(list)
	return list[rand(list.size())]
end

#randchoices(list, amount) ⇒ Object

Every element in the given list will be in the returned list no more than 1 time.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/main.rb', line 23

def randchoices(list, amount)
	return nil if amount > list.size()	
	
	return_list = []
	for i in 1..amount
		index = rand(list.size())
		choice = list[index]
		return_list << choice
		list.delete_at(index)
	end
	return return_list
end

#randhashkey(hash) ⇒ Object

Returns a random key from the given hash.



77
78
79
# File 'lib/main.rb', line 77

def randhashkey(hash)
	return randchoice(hash.keys())
end

#randhashkeys(hash, amount) ⇒ Object

Returns the specified amount of random hash keys.



81
82
83
# File 'lib/main.rb', line 81

def randhashkeys(hash, amount)
	return randchoices(hash.keys(), amount)
end

#randhashvalue(hash) ⇒ Object

Returns a random value from the given hash.



85
86
87
# File 'lib/main.rb', line 85

def randhashvalue(hash)
	return randchoice(hash.values())
end

#randhashvalues(hash, amount) ⇒ Object

Returns the specified amount of random values from the given hash.



89
90
91
# File 'lib/main.rb', line 89

def randhashvalues(hash, amount)
	return randchoices(hash.values(), amount)
end

#randint(min, max) ⇒ Object

Returns a random number between min and max(inclusive of both end points).



9
10
11
# File 'lib/main.rb', line 9

def randint(min, max)
	return rand(max - min + 1) + min
end

#randints(min, max, amount) ⇒ Object

There will not be repeated numbers.



14
15
16
# File 'lib/main.rb', line 14

def randints(min, max, amount)
	return randchoices((min..max).to_a(), amount)
end

#randletter(uppercase = true, lowercase = true, numbers_included = false) ⇒ Object

THIS METHOD IS NOT YET IMPLEMENTED AND SHOULD NOT BE USED. Returns a random ascii letter.

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/main.rb', line 56

def randletter(uppercase=true, lowercase=true, numbers_included=false)
	raise NotImplementedError.new("Method randletter in module Randomness not yet implemented.")
end

#randletters(amount, uppercase = true, lowercase = true, numbers_included = false) ⇒ Object

THIS METHOD IS NOT YET IMPLEMENTED AND SHOULD NOT BE USED. Returns the specified amount of random ascii letters.

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/main.rb', line 60

def randletters(amount, uppercase=true, lowercase=true, numbers_included=false)
	raise NotImplementedError.new("Method randletters in module Randomness not yet implemented.")
end

#randomizeseedObject

Sets the random seed to a value based on the current time.



4
5
6
7
# File 'lib/main.rb', line 4

def randomizeseed()
	time = Time.now()
	srand(time.yday * time.usec * time.sec)
end

#randselection(list) ⇒ Object

Returns a random number of random elements from the provided list.



36
37
38
# File 'lib/main.rb', line 36

def randselection(list)
	return randchoices(list, randint(1, list.size()))
end

#randword(string) ⇒ Object

Returns a random word from a string.



40
41
42
# File 'lib/main.rb', line 40

def randword(string)
	return randchoice(string.split(' '))
end

#randwords(string, amount) ⇒ Object

Returns the specified amount of randomly chosen words.



44
45
46
# File 'lib/main.rb', line 44

def randwords(string, amount)
	return randchoices(string.split(' '), amount)
end

#shufflearray(array) ⇒ Object

Shuffles the array and returns the result.



93
94
95
# File 'lib/main.rb', line 93

def shufflearray(array)
	return array.shuffle()
end

#shufflehash(hash) ⇒ Object

Shuffles the key-value mapping in the given hash and returns the result.



97
98
99
# File 'lib/main.rb', line 97

def shufflehash(hash)
	return hash.values().shuffle()
end