module Randomness
The main module, meant to be a namespace and not a mixin.
Public Instance Methods
Returns a random boolean value.
# File lib/randomness.rb, line 78 def randbool() return true if randint(1, 2) == 1 return false end
Returns the specified number of randomly chosen boolean values.
# File lib/randomness.rb, line 83 def randbools(amount) return_list = [] for i in 1..amount return_list << randbool() end return return_list end
Returns a random character from a string.
# File lib/randomness.rb, line 48 def randchar(string) return randchoice(string.split('')) end
Returns the specified amount of randomly chosen characters from the string.
# File lib/randomness.rb, line 52 def randchars(string, amount) return randchoices(string.split(''), amount) end
Returns a random selection from the list.
# File lib/randomness.rb, line 18 def randchoice(list) return list[rand(list.size())] end
Returns the specified number of choices from the list. Returns nil if the amount is greater than the list size. Every element in the given list will be in the returned list no more than 1 time.
# File lib/randomness.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
Returns a random key from the given hash.
# File lib/randomness.rb, line 91 def randhashkey(hash) return randchoice(hash.keys()) end
Returns the specified amount of random hash keys.
# File lib/randomness.rb, line 95 def randhashkeys(hash, amount) return randchoices(hash.keys(), amount) end
Returns a random value from the given hash.
# File lib/randomness.rb, line 99 def randhashvalue(hash) return randchoice(hash.values()) end
Returns the specified amount of random values from the given hash.
# File lib/randomness.rb, line 103 def randhashvalues(hash, amount) return randchoices(hash.values(), amount) end
Returns a random number between min and max(inclusive of both end points).
# File lib/randomness.rb, line 9 def randint(min, max) return rand(max - min + 1) + min end
Returns the specified amount of random numbers between min and max(inclusive of both end points). There will not be repeated numbers.
# File lib/randomness.rb, line 14 def randints(min, max, amount) return randchoices((min..max).to_a(), amount) end
Returns a random ascii letter.
# File lib/randomness.rb, line 56 def randletter(uppercase=true, lowercase=true, numbers_included=false) letters = [] if uppercase letters += ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')) end if lowercase letters += 'abcdefghijklmnopqrstuvwxyz'.split('') end if numbers_included letters += '0123456789' end return randchoice(letters) end
Returns the specified amount of random ascii letters. The returned list may have repeats.
# File lib/randomness.rb, line 70 def randletters(amount, uppercase=true, lowercase=true, numbers_included=false) return_list = [] for i in 1..amount return_list << randletter(uppercase, lowercase, numbers_included) end return return_list end
Sets the random seed to a value based on the current time.
# File lib/randomness.rb, line 4 def randomizeseed() time = Time.now() srand(time.yday * time.usec * time.sec) end
Returns a random number of random elements from the provided list.
# File lib/randomness.rb, line 36 def randselection(list) return randchoices(list, randint(1, list.size())) end
Returns a random word from a string.
# File lib/randomness.rb, line 40 def randword(string) return randchoice(string.split(' ')) end
Returns the specified amount of randomly chosen words.
# File lib/randomness.rb, line 44 def randwords(string, amount) return randchoices(string.split(' '), amount) end
Shuffles the array and returns the result.
# File lib/randomness.rb, line 107 def shufflearray(array) return array.shuffle() end
Shuffles the array.
# File lib/randomness.rb, line 111 def shufflearray!(array) array.shuffle!() end
Shuffles the key-value mapping in the given hash and returns the result.
# File lib/randomness.rb, line 115 def shufflehash(hash) return hash.values().shuffle() end
Shuffles the key-value mapping in the given hash.
# File lib/randomness.rb, line 119 def shufflehash!(hash) hash.values().shuffle!() end