It is possible to generate pseudo-random numbers in jq
if you provide one initial random number (--argjson initialRandomNumber
).
Passing $RANDOM$RANDOM
instead of $RANDOM
is intended to increase the range for the initial pseudo-random value.
I used a slightly modified version of the function nextRandomNumber
from Rosettacode jq: random numbers
to generate random numbers, Strings and UUIDs as shown in the following code.
Each function takes a parameter $state
and delivers a newState
in the response for a subsequent call.
Because you ask how to generate a
- random number
- random string
- UUID
you can find 6 functions in my code to generate a single instance and an array of them.
You can pick the function you need and use it as a filter in jq
.
#!/bin/bash
jq -c -r -n --argjson initialRandomNumber "$RANDOM$RANDOM" '
# 15-bit integers generated using the same formula as rand() from the Microsoft C Runtime.
# The random numbers are in [0 -- 32767] inclusive.
#
# Input:
# first call: $state = a random number provided to jq by parameter
# subsequent call: $state = "newState" from last response
#
# Output:
# object with pseudo-random number and "newState" for a subsequent call.
def nextRandomNumber($state):
( (214013 * $state) + 2531011) % 2147483648 # mod 2^31
| { newState: .,
randomNumber: (. / 65536 | floor) };
def nextRandomNumbers($state; $count):
[foreach range($count) as $x (nextRandomNumber($state); nextRandomNumber(.newState); .)]
| { newState: .[-1].newState,
randomNumbers: map(.randomNumber) };
# ----- random UUID ---------------
def hexByte:
[. / 256 % 16, . % 16]
| map(if . < 10 then . + 48 else . + 87 end) # ASCII: 0...9: 48-57, a...f: 97-102
| implode;
def nextRandomUUID($state):
nextRandomNumbers($state; 16)
| .newState as $newState
| .randomNumbers
| map(hexByte)
| "\(.[0:4] | join(""))-\(.[4:6] | join(""))-\(.[6:8] | join(""))-\(.[8:10] | join(""))-\(.[10:] | join(""))"
| { newState: $newState,
randomUUID: . };
def nextRandomUUIDs($state; $count):
[foreach range($count) as $x (nextRandomUUID($state); nextRandomUUID(.newState); .)]
| { newState: .[-1].newState,
randomUUIDs: map(.randomUUID) };
# ----- random String ---------------
def letter:
. % 52
| [if . < 26 then . + 65 else . + 71 end] # ASCII: A...Z: 65-90, a...z: 97-122
| implode;
def nextRandomString($state; $minLength; $maxLength):
nextRandomNumber($state)
| (try (.randomNumber % ($maxLength - $minLength + 1) + $minLength) catch $minLength) as $length
| nextRandomNumbers(.newState; $length)
| .newState as $newState
| .randomNumbers
| map(letter)
| join("")
| { newState: $newState,
randomString: . };
def nextRandomStrings($state; $count; $minLength; $maxLength):
[foreach range($count) as $x (nextRandomString($state; $minLength; $maxLength); nextRandomString(.newState; $minLength; $maxLength); .)]
| { newState: .[-1].newState,
randomStrings: map(.randomString) };
# ----- example usage ---------------
nextRandomNumber($initialRandomNumber) # see output 1
# nextRandomNumbers($initialRandomNumber; 3) # see output 2
# nextRandomUUID($initialRandomNumber) # see output 3
# nextRandomUUIDs($initialRandomNumber; 3) # see output 4
# nextRandomString($initialRandomNumber; 10; 15) # see output 5
# nextRandomStrings($initialRandomNumber; 3; 6; 10) # see output 6
# nextRandomNumber($initialRandomNumber) | nextRandomNumbers(.newState; 3) # see output 7
'
Outputs
output 1
: generate pseudo-random number
{"newState":912028498,"randomNumber":13916}
output 2
: generate 3 pseudo-random numbers
{"newState":677282016,"randomNumbers":[10202,20943,6980]}`
output 3
: generate random UUID
{"newState":1188119770,"randomUUID":"cdcda95b-af57-1303-da72-d21c6e7b1861"}
output 4
: generate 3 random UUIDs
{"newState":907540185,"randomUUIDs":["855c1445-b529-4301-a535-20cb298feaff","5b685e49-8596-830e-f56a-0a22c43c4c32","35fed6d8-d72b-2833-fd6f-f99154358067"]}
output 5
: generate random Strings with length 10-15
{"newState":1037126684,"randomString":"SJadqPGkERAu"}`
output 6
: generate 3 random Strings with length 6-10
{"newState":316121190,"randomStrings":["eNKxechu","XPkvNg","TIABHbYCxB"]}`
output 7
: using newState
for a second call to generate 3 random numbers
{"newState":808494511,"randomNumbers":[26045,16811,12336]}`