aboutsummaryrefslogtreecommitdiff
path: root/markov-run.rb
diff options
context:
space:
mode:
authorNathan Lasseter <nathan@4574.co.uk>2014-01-14 17:55:47 +0000
committerNathan Lasseter <nathan@4574.co.uk>2014-01-14 17:55:47 +0000
commit79a5cf84254c2aeed244861197fdc5fe3282a940 (patch)
treec9b3436e145dffb33904760d4a80fb723242c12c /markov-run.rb
First Commit
Diffstat (limited to 'markov-run.rb')
-rwxr-xr-xmarkov-run.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/markov-run.rb b/markov-run.rb
new file mode 100755
index 0000000..ef81315
--- /dev/null
+++ b/markov-run.rb
@@ -0,0 +1,35 @@
+#!/usr/bin/env ruby
+
+PRNG = Random.new(Time.now.to_i)
+
+stats = Marshal::load(File.open(ARGV[0]).read)
+
+def cfd(hash)
+ tot = 0
+ hash.each_pair {|k, v|
+ hash[k] = tot += v
+ }
+end
+
+def sel(hash)
+ max = hash.values.max
+ r = PRNG.rand(max*100000) % max
+ hash.each_pair {|k, v|
+ return k if r < v
+ }
+end
+
+current = stats.keys.select{|k|k.first.match(/^[A-Z]/)}.sample
+
+print "#{current.join(" ")} "
+
+def achunk(chunk, hash)
+ sel(cfd(hash[chunk]))
+end
+
+(ARGV[1].to_i - 1).times do
+ current = achunk(current, stats)
+ print "#{current.join(" ")} "
+end
+
+puts