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