Puppet Function: seeded_rand

Defined in:
vendor_modules/stdlib/lib/puppet/parser/functions/seeded_rand.rb
Function type:
Ruby 3.x API

Summary

Generates a random whole number greater than or equal to 0 and less than MAX, using the value of SEED for repeatable randomness.

Overview

seeded_rand()Any

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`.

Examples:

*Usage:*

seeded_rand(MAX, SEED).
MAX must be a positive integer; SEED is any string.

Returns:

  • (Any)

    random number greater than or equal to 0 and less than MAX



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'vendor_modules/stdlib/lib/puppet/parser/functions/seeded_rand.rb', line 6

Puppet::Parser::Functions.newfunction(:seeded_rand, arity: 2, type: :rvalue, doc: <<-DOC
    @summary
      Generates a random whole number greater than or equal to 0 and less than MAX, using the value of SEED for repeatable randomness.

    @return
      random number greater than or equal to 0 and less than MAX

    @example **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`.
DOC
) do |args|
  require 'digest/md5'

  raise(ArgumentError, 'seeded_rand(): first argument must be a positive integer') unless function_is_integer([args[0]]) && 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_int(seed, max)
end