Module: Randomness

Defined in:
lib/randomness.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.



78
79
80
81
# File 'lib/randomness.rb', line 78

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

#randbools(amount) ⇒ Object

Returns the specified number of randomly chosen boolean values.



83
84
85
86
87
88
89
# File 'lib/randomness.rb', line 83

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/randomness.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/randomness.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/randomness.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/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

#randhashkey(hash) ⇒ Object

Returns a random key from the given hash.



91
92
93
# File 'lib/randomness.rb', line 91

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

#randhashkeys(hash, amount) ⇒ Object

Returns the specified amount of random hash keys.



95
96
97
# File 'lib/randomness.rb', line 95

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

#randhashvalue(hash) ⇒ Object

Returns a random value from the given hash.



99
100
101
# File 'lib/randomness.rb', line 99

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

#randhashvalues(hash, amount) ⇒ Object

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



103
104
105
# File 'lib/randomness.rb', line 103

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/randomness.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/randomness.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

Returns a random ascii letter.



56
57
58
59
60
61
62
63
64
65
66
67
68
# 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

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

Returns the specified amount of random ascii letters. The returned list may have repeats.



70
71
72
73
74
75
76
# 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

#randomizeseedObject

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



4
5
6
7
# File 'lib/randomness.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/randomness.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/randomness.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/randomness.rb', line 44

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

#shufflearray(array) ⇒ Object

Shuffles the array and returns the result.



107
108
109
# File 'lib/randomness.rb', line 107

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

#shufflearray!(array) ⇒ Object

Shuffles the array.



111
112
113
# File 'lib/randomness.rb', line 111

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

#shufflehash(hash) ⇒ Object

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



115
116
117
# File 'lib/randomness.rb', line 115

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

#shufflehash!(hash) ⇒ Object

Shuffles the key-value mapping in the given hash.



119
120
121
# File 'lib/randomness.rb', line 119

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