Class: Eventsims::Generate

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

Overview

Generating random outcomes and probabilities

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Generate

Returns a new instance of Generate.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
# File 'lib/eventsims/randgen.rb', line 7

def initialize(*args)
  ''' Initialisation of all values'''
  # Starting variables that change result depending on the if statements
  start, stop, jump = 0, 10, 1
  @@outcome, @@occur, @@unique, @@problist, @@cumprob = [], [], [], [], []
  amount = (2..20).step(jump).to_a.sample
  amount.times{@@outcome << (start..stop).step(jump).to_a.sample}
  
# If statements to run different functions
  if args.size == 0
    @@outcome = @@outcome

  elsif args.size == 1
    if args[0].is_a?(Integer) 
      amount, @@outcome = args[0], []
      amount.times{@@outcome << (start..stop).step(jump).to_a.sample}
      
    elsif args[0] == "r"
      @@outcome.sort!{|x,y| y<=>x}

    elsif args[0] == "s"
      @@outcome.sort!

    else
      raise "\nInvalid argument: Use (amount, 'r' or 's')"
    end

  elsif args.size == 2
    if args.all?{|x| x.is_a?(Integer)}
      start, stop, @@outcome = args[0], args[1], []
      amount.times{@@outcome << (start..stop).step(jump).to_a.sample} 
      
    elsif args[1] == "r"
      amount, @@outcome = args[0], []
      amount.times{@@outcome << (start..stop).step(jump).to_a.sample}
      @@outcome.sort!{|x,y| y<=>x}
      
    elsif args[1] == "s"
      amount, @@outcome = args[0], []
      amount.times{@@outcome << (start..stop).step(jump).to_a.sample}
      @@outcome.sort!

    else
      raise "\nInvalid argument: Use (start, stop) or (amount, 'r' or 's')'"
    end

  elsif args.size == 3
    if args.all?{|x| x.is_a?(Integer)}
      start, stop, amount, @@outcome = args[0], args[1], args[2], []
      amount.times{@@outcome << (start..stop).step(jump).to_a.sample} 
      
    elsif args[2] == "r"
      start, stop, @@outcome = args[0], args[1], []
      amount.times{@@outcome << (start..stop).step(jump).to_a.sample} 
      @@outcome.sort!{|x,y| y<=>x}

    elsif args[2] == "s"
      start, stop, @@outcome = args[0], args[1], []
      amount.times{@@outcome << (start..stop).step(jump).to_a.sample} 
      @@outcome.sort!

    else
      raise "\nInvalid argument: Use (start, stop, amount) or (amount, 'r' or 's')'"
    end

  elsif args.size == 4
    if args.all?{|x| x.is_a?(Integer)}
      start, stop, jump, amount, @@outcome = args[0], args[1], args[2], args[3], []
      amount.times{@@outcome << (start..stop).step(jump).to_a.sample}
      
    elsif args[3] == "r"
      start, stop, amount, @@outcome = args[0], args[1], args[2], []
      amount.times{@@outcome << (start..stop).step(jump).to_a.sample}
      @@outcome.sort!{|x,y| y<=>x}

    elsif args[3] == "s"
      start, stop, amount, @@outcome = args[0], args[1], args[2], []
      amount.times{@@outcome << (start..stop).step(jump).to_a.sample}
      @@outcome.sort!

    else
      raise "\nInvalid argument: Use (start, stop, step, amount) or \n\t(start, stop, amount, 'r' or 's')'"
    end

  elsif args.size == 5 
    if args[4] == "r"
      start, stop, jump, amount, @@outcome = args[0], args[1], args[2], args[3], []
      amount.times{@@outcome << (start..stop).step(jump).to_a.sample}
      @@outcome.sort!{|x,y| y<=>x}

    elsif args[4] == "s"
      start, stop, jump, amount, @@outcome = args[0], args[1], args[2], args[3], []
      amount.times{@@outcome << (start..stop).step(jump).to_a.sample}
      @@outcome.sort!

    else
      raise "\nInvalid argument: Use (start, stop, step, amount, 'r' or 's')'"
    end
  end

  #Generates a set and times of occurrence ofoutcome
  @@outcome.each{|item| @@unique << item  unless @@unique.include?(item)}
  result = @@outcome.each_with_object(Hash.new(0)) { |word, counts| counts[word] += 1 }
  result.each{|x,y| @@occur << y}
  
end

Instance Method Details

#getcumObject

Returns a cummulative probability list of all items in its argument



124
125
126
127
128
129
130
131
# File 'lib/eventsims/randgen.rb', line 124

def getcum()
  '''Returns a cummulative probability list of all items in its argument'''
  increase, probability, cumlist = 0, getprob(), []
  probability.each{|i| increase +=i
    cumlist << increase.round(4) }
  cumlist[-1] = 1.0      #makes sure the last value is 1.0
  return cumlist
end

#getprobObject

Returns a probability list of all items in its argument



115
116
117
118
119
120
121
# File 'lib/eventsims/randgen.rb', line 115

def getprob()
  probability = []
  '''Returns a probability list of all items in its argument'''
  @@occur.each{|i| probability << (i*1.0/@@outcome.size).round(4)}

  return probability
end

#occurObject

# Returns the number of times the unique item is found



146
147
148
149
# File 'lib/eventsims/randgen.rb', line 146

def occur
  '''Returns the number of times the unique item is found'''
  return @@occur
end

#outcomeObject

Returns the outcome



134
135
136
137
# File 'lib/eventsims/randgen.rb', line 134

def outcome()
  ''' Returns a generated outcome '''
  return @@outcome
end

#uniqueObject

Returns the unique outcome



140
141
142
143
# File 'lib/eventsims/randgen.rb', line 140

def unique()
  '''Returns the unique outcome'''
  return @@unique
end