github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/docs/content/functions/random.md (about) 1 --- 2 title: random functions 3 menu: 4 main: 5 parent: functions 6 --- 7 8 Functions for generating random values. 9 10 ### About randomness 11 12 `gomplate` uses Go's [`math/rand`](https://golang.org/pkg/math/rand/) package 13 to generate pseudo-random numbers. Note that these functions are not suitable 14 for use in security-sensitive applications, such as cryptography. However, 15 these functions will not deplete system entropy. 16 17 ## `random.ASCII` 18 19 Generates a random string of a desired length, containing the set of 20 printable characters from the 7-bit [ASCII](https://en.wikipedia.org/wiki/ASCII) 21 set. This includes _space_ (' '), but no other whitespace characters. 22 23 _Added in gomplate [v3.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.4.0)_ 24 ### Usage 25 26 ``` 27 random.ASCII count 28 ``` 29 30 ### Arguments 31 32 | name | description | 33 |------|-------------| 34 | `count` | _(required)_ the length of the string to produce (number of characters) | 35 36 ### Examples 37 38 ```console 39 $ gomplate -i '{{ random.ASCII 8 }}' 40 _woJ%D&K 41 ``` 42 43 ## `random.Alpha` 44 45 Generates a random alphabetical (`A-Z`, `a-z`) string of a desired length. 46 47 _Added in gomplate [v3.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.4.0)_ 48 ### Usage 49 50 ``` 51 random.Alpha count 52 ``` 53 54 ### Arguments 55 56 | name | description | 57 |------|-------------| 58 | `count` | _(required)_ the length of the string to produce (number of characters) | 59 60 ### Examples 61 62 ```console 63 $ gomplate -i '{{ random.Alpha 42 }}' 64 oAqHKxHiytYicMxTMGHnUnAfltPVZDhFkVkgDvatJK 65 ``` 66 67 ## `random.AlphaNum` 68 69 Generates a random alphanumeric (`0-9`, `A-Z`, `a-z`) string of a desired length. 70 71 _Added in gomplate [v3.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.4.0)_ 72 ### Usage 73 74 ``` 75 random.AlphaNum count 76 ``` 77 78 ### Arguments 79 80 | name | description | 81 |------|-------------| 82 | `count` | _(required)_ the length of the string to produce (number of characters) | 83 84 ### Examples 85 86 ```console 87 $ gomplate -i '{{ random.AlphaNum 16 }}' 88 4olRl9mRmVp1nqSm 89 ``` 90 91 ## `random.String` 92 93 Generates a random string of a desired length. 94 95 By default, the possible characters are those represented by the 96 regular expression `[a-zA-Z0-9_.-]` (alphanumeric, plus `_`, `.`, and `-`). 97 98 A different set of characters can be specified with a regular expression, 99 or by giving a range of possible characters by specifying the lower and 100 upper bounds. Lower/upper bounds can be specified as characters (e.g. 101 `"q"`, or escape sequences such as `"\U0001f0AF"`), or numeric Unicode 102 code-points (e.g. `48` or `0x30` for the character `0`). 103 104 When given a range of Unicode code-points, `random.String` will discard 105 non-printable characters from the selection. This may result in a much 106 smaller set of possible characters than intended, so check 107 the [Unicode character code charts](http://www.unicode.org/charts/) to 108 verify the correct code-points. 109 110 _Added in gomplate [v3.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.4.0)_ 111 ### Usage 112 113 ``` 114 random.String count [regex] [lower] [upper] 115 ``` 116 117 ### Arguments 118 119 | name | description | 120 |------|-------------| 121 | `count` | _(required)_ the length of the string to produce (number of characters) | 122 | `regex` | _(optional)_ the regular expression that each character must match (defaults to `[a-zA-Z0-9_.-]`) | 123 | `lower` | _(optional)_ lower bound for a range of characters (number or single character) | 124 | `upper` | _(optional)_ upper bound for a range of characters (number or single character) | 125 126 ### Examples 127 128 ```console 129 $ gomplate -i '{{ random.String 8 }}' 130 FODZ01u_ 131 ``` 132 ```console 133 $ gomplate -i '{{ random.String 16 `[[:xdigit:]]` }}' 134 B9e0527C3e45E1f3 135 ``` 136 ```console 137 $ gomplate -i '{{ random.String 20 `[\p{Canadian_Aboriginal}]` }}' 138 ᗄᖖᣡᕔᕫᗝᖴᒙᗌᘔᓰᖫᗵᐕᗵᙔᗠᓅᕎᔹ 139 ``` 140 ```console 141 $ gomplate -i '{{ random.String 8 "c" "m" }}' 142 ffmidgjc 143 ``` 144 ```console 145 $ gomplate -i 'You rolled... {{ random.String 3 "⚀" "⚅" }}' 146 You rolled... ⚅⚂⚁ 147 ``` 148 ```console 149 $ gomplate -i 'Poker time! {{ random.String 5 "\U0001f0a1" "\U0001f0de" }}' 150 Poker time! 🂼🂺🂳🃅🂪 151 ``` 152 153 ## `random.Item` 154 155 Pick an element at a random from a given slice or array. 156 157 _Added in gomplate [v3.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.4.0)_ 158 ### Usage 159 160 ``` 161 random.Item items 162 ``` 163 ``` 164 items | random.Item 165 ``` 166 167 ### Arguments 168 169 | name | description | 170 |------|-------------| 171 | `items` | _(required)_ the input array | 172 173 ### Examples 174 175 ```console 176 $ gomplate -i '{{ random.Item (seq 0 5) }}' 177 4 178 ``` 179 ```console 180 $ export SLICE='["red", "green", "blue"]' 181 $ gomplate -i '{{ getenv "SLICE" | jsonArray | random.Item }}' 182 blue 183 ``` 184 185 ## `random.Number` 186 187 Pick a random integer. By default, a number between `0` and `100` 188 (inclusive) is chosen, but this range can be overridden. 189 190 Note that the difference between `min` and `max` can not be larger than a 191 63-bit integer (i.e. the unsigned portion of a 64-bit signed integer). 192 The result is given as an `int64`. 193 194 _Added in gomplate [v3.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.4.0)_ 195 ### Usage 196 197 ``` 198 random.Number [min] [max] 199 ``` 200 201 ### Arguments 202 203 | name | description | 204 |------|-------------| 205 | `min` | _(optional)_ The minimum value, defaults to `0`. Must be less than `max`. | 206 | `max` | _(optional)_ The maximum value, defaults to `100` (if no args provided) | 207 208 ### Examples 209 210 ```console 211 $ gomplate -i '{{ random.Number }}' 212 55 213 ``` 214 ```console 215 $ gomplate -i '{{ random.Number -10 10 }}' 216 -3 217 ``` 218 ```console 219 $ gomplate -i '{{ random.Number 5 }}' 220 2 221 ``` 222 223 ## `random.Float` 224 225 Pick a random decimal floating-point number. By default, a number between 226 `0.0` and `1.0` (_exclusive_, i.e. `[0.0,1.0)`) is chosen, but this range 227 can be overridden. 228 229 The result is given as a `float64`. 230 231 _Added in gomplate [v3.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.4.0)_ 232 ### Usage 233 234 ``` 235 random.Float [min] [max] 236 ``` 237 238 ### Arguments 239 240 | name | description | 241 |------|-------------| 242 | `min` | _(optional)_ The minimum value, defaults to `0.0`. Must be less than `max`. | 243 | `max` | _(optional)_ The maximum value, defaults to `1.0` (if no args provided). | 244 245 ### Examples 246 247 ```console 248 $ gomplate -i '{{ random.Float }}' 249 0.2029946480303966 250 ``` 251 ```console 252 $ gomplate -i '{{ random.Float 100 }}' 253 71.28595374161743 254 ``` 255 ```console 256 $ gomplate -i '{{ random.Float -100 200 }}' 257 105.59119437834909 258 ```