Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / seeded_rand.rb
1 Puppet::Parser::Functions::newfunction(
2   :seeded_rand,
3   :arity => 2,
4   :type => :rvalue,
5   :doc => <<-EOS
6 Usage: `seeded_rand(MAX, SEED)`. MAX must be a positive integer; SEED is any string.
7
8 Generates a random whole number greater than or equal to 0 and less
9 than MAX, using the value of SEED for repeatable randomness.  If SEED
10 starts with "$fqdn:", this is behaves the same as `fqdn_rand`.
11
12 EOS
13 ) do |args|
14   require 'digest/md5'
15
16   raise(ArgumentError, "seeded_rand(): first argument must be a positive integer") unless function_is_integer([args[0]]) and args[0].to_i > 0
17   raise(ArgumentError, "seeded_rand(): second argument must be a string") unless args[1].is_a? String
18
19   max = args[0].to_i
20   seed = Digest::MD5.hexdigest(args[1]).hex
21   Puppet::Util.deterministic_rand(seed,max)
22 end