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