File: seeded_rand.rb

package info (click to toggle)
puppet-module-puppetlabs-stdlib 9.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,448 kB
  • sloc: ruby: 3,522; sh: 46; makefile: 2
file content (22 lines) | stat: -rw-r--r-- 668 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# frozen_string_literal: true

# @summary
#   Generates a random whole number greater than or equal to 0 and less than max, using the value of seed for repeatable randomness.
Puppet::Functions.create_function(:'stdlib::seeded_rand') do
  # @param max The maximum value.
  # @param seed The seed used for repeatable randomness.
  #
  # @return [Integer]
  #   A random number greater than or equal to 0 and less than max
  dispatch :seeded_rand do
    param 'Integer[1]', :max
    param 'String', :seed
  end

  def seeded_rand(max, seed)
    require 'digest/md5'

    seed = Digest::MD5.hexdigest(seed).hex
    Puppet::Util.deterministic_rand_int(seed, max)
  end
end