4 module Puppet::Parser::Functions
5 newfunction(:shuffle, :type => :rvalue, :doc => <<-DOC
7 Randomizes the order of a string or array elements.
10 randomized string or array
14 raise(Puppet::ParseError, "shuffle(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
18 unless value.is_a?(Array) || value.is_a?(String)
19 raise(Puppet::ParseError, 'shuffle(): Requires either array or string to work with')
24 string = value.is_a?(String) ? true : false
26 # Check whether it makes sense to shuffle ...
27 return result if result.size <= 1
29 # We turn any string value into an array to be able to shuffle ...
30 result = string ? result.split('') : result
32 elements = result.size
34 # Simple implementation of Fisher–Yates in-place shuffle ...
36 j = rand(elements - i) + i
37 result[j], result[i] = result[i], result[j]
40 result = string ? result.join : result
46 # vim: set ts=2 sw=2 et :