Module: Randomness
- Defined in:
- lib/main.rb
Overview
The main module, meant to be a namespace and not a mixin.
Instance Method Summary collapse
-
#randbool ⇒ Object
Returns a random boolean value.
-
#randbools(amount) ⇒ Object
Returns the specified number of randomly chosen boolean values.
-
#randchar(string) ⇒ Object
Returns a random character from a string.
-
#randchars(string, amount) ⇒ Object
Returns the specified amount of randomly chosen characters from the string.
-
#randchoice(list) ⇒ Object
Returns a random selection from the list.
-
#randchoices(list, amount) ⇒ Object
Every element in the given list will be in the returned list no more than 1 time.
-
#randhashkey(hash) ⇒ Object
Returns a random key from the given hash.
-
#randhashkeys(hash, amount) ⇒ Object
Returns the specified amount of random hash keys.
-
#randhashvalue(hash) ⇒ Object
Returns a random value from the given hash.
-
#randhashvalues(hash, amount) ⇒ Object
Returns the specified amount of random values from the given hash.
-
#randint(min, max) ⇒ Object
Returns a random number between min and max(inclusive of both end points).
-
#randints(min, max, amount) ⇒ Object
There will not be repeated numbers.
-
#randletter(uppercase = true, lowercase = true, numbers_included = false) ⇒ Object
THIS METHOD IS NOT YET IMPLEMENTED AND SHOULD NOT BE USED.
-
#randletters(amount, uppercase = true, lowercase = true, numbers_included = false) ⇒ Object
THIS METHOD IS NOT YET IMPLEMENTED AND SHOULD NOT BE USED.
-
#randomizeseed ⇒ Object
Sets the random seed to a value based on the current time.
-
#randselection(list) ⇒ Object
Returns a random number of random elements from the provided list.
-
#randword(string) ⇒ Object
Returns a random word from a string.
-
#randwords(string, amount) ⇒ Object
Returns the specified amount of randomly chosen words.
-
#shufflearray(array) ⇒ Object
Shuffles the array and returns the result.
-
#shufflehash(hash) ⇒ Object
Shuffles the key-value mapping in the given hash and returns the result.
Instance Method Details
#randbool ⇒ Object
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.
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.
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 |
#randomizeseed ⇒ Object
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 |