summaryrefslogtreecommitdiff
path: root/lib/puppet
diff options
context:
space:
mode:
authorBryan Jen <bryan.jen@gmail.com>2015-12-10 11:54:10 -0700
committerBryan Jen <bryan.jen@gmail.com>2015-12-10 11:54:10 -0700
commit93600e45b006eeebfa012b4f8ffd7ca31643d330 (patch)
tree8ad6625ced8bca2334583af6cdce70579f809476 /lib/puppet
parent7a745deec698b16a0702d00798dc30dd6c685312 (diff)
parent8aecd63378f6dc3aeafe71d91212f613aa0bb829 (diff)
downloadpuppet-stdlib-93600e45b006eeebfa012b4f8ffd7ca31643d330.tar.gz
puppet-stdlib-93600e45b006eeebfa012b4f8ffd7ca31643d330.tar.bz2
Merge pull request #554 from kjetilho/ticket/2886-seeded_rand
(#2886) seeded_rand: new function
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/parser/functions/seeded_rand.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/seeded_rand.rb b/lib/puppet/parser/functions/seeded_rand.rb
new file mode 100644
index 0000000..44e27b8
--- /dev/null
+++ b/lib/puppet/parser/functions/seeded_rand.rb
@@ -0,0 +1,22 @@
+Puppet::Parser::Functions::newfunction(
+ :seeded_rand,
+ :arity => 2,
+ :type => :rvalue,
+ :doc => <<-EOS
+Usage: `seeded_rand(MAX, SEED)`. MAX must be a positive integer; SEED is any string.
+
+Generates a random whole number greater than or equal to 0 and less
+than MAX, using the value of SEED for repeatable randomness. If SEED
+starts with "$fqdn:", this is behaves the same as `fqdn_rand`.
+
+EOS
+) do |args|
+ require 'digest/md5'
+
+ raise(ArgumentError, "seeded_rand(): first argument must be a positive integer") unless function_is_integer([args[0]]) and args[0].to_i > 0
+ raise(ArgumentError, "seeded_rand(): second argument must be a string") unless args[1].is_a? String
+
+ max = args[0].to_i
+ seed = Digest::MD5.hexdigest(args[1]).hex
+ Puppet::Util.deterministic_rand(seed,max)
+end