Mercurial > repos > other > Puppet
comparison modules/stdlib/lib/puppet/parser/functions/seeded_rand.rb @ 272:c42fb28cff86
Update to a newer Python module
This also pulls in an EPEL module (which we don't use) and a newer
stdlib version.
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 03 Jan 2020 19:56:04 +0000 |
parents | |
children | d9352a684e62 |
comparison
equal
deleted
inserted
replaced
271:c62728474654 | 272:c42fb28cff86 |
---|---|
1 # | |
2 # seeded_rand.rb | |
3 # | |
4 Puppet::Parser::Functions.newfunction( | |
5 :seeded_rand, | |
6 :arity => 2, | |
7 :type => :rvalue, | |
8 :doc => <<-DOC | |
9 Usage: `seeded_rand(MAX, SEED)`. MAX must be a positive integer; SEED is any string. | |
10 | |
11 Generates a random whole number greater than or equal to 0 and less | |
12 than MAX, using the value of SEED for repeatable randomness. If SEED | |
13 starts with "$fqdn:", this is behaves the same as `fqdn_rand`. | |
14 DOC | |
15 ) do |args| | |
16 require 'digest/md5' | |
17 | |
18 raise(ArgumentError, 'seeded_rand(): first argument must be a positive integer') unless function_is_integer([args[0]]) && args[0].to_i > 0 | |
19 raise(ArgumentError, 'seeded_rand(): second argument must be a string') unless args[1].is_a? String | |
20 | |
21 max = args[0].to_i | |
22 seed = Digest::MD5.hexdigest(args[1]).hex | |
23 Puppet::Util.deterministic_rand(seed, max) | |
24 end |