source: other-projects/hathitrust/vagrant-solr-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/seeded_rand.rb@ 30960

Last change on this file since 30960 was 30960, checked in by davidb, 7 years ago

Switch to using Puppet to provision machine. Strongly based on files developed for spark-hdfs cluster

File size: 794 bytes
Line 
1Puppet::Parser::Functions::newfunction(
2 :seeded_rand,
3 :arity => 2,
4 :type => :rvalue,
5 :doc => <<-EOS
6Usage: `seeded_rand(MAX, SEED)`. MAX must be a positive integer; SEED is any string.
7
8Generates a random whole number greater than or equal to 0 and less
9than MAX, using the value of SEED for repeatable randomness. If SEED
10starts with "$fqdn:", this is behaves the same as `fqdn_rand`.
11
12EOS
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)
22end
Note: See TracBrowser for help on using the repository browser.