Skip to content

Commit 87424c8

Browse files
authored
Merge pull request #813 from gradle/erichaagdev/more-random-id
Experiment `run_id` is made significantly more random
2 parents c9eaf83 + 59336a4 commit 87424c8

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

components/scripts/lib/init.sh

+26-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,33 @@ make_symlink_to_latest_experiment_dir() {
1414
ln -s "${EXP_DIR}" "${LINK_NAME}" > /dev/null 2>&1
1515
}
1616

17+
# Below is an explanation of what constitutes a 'run_id'.
18+
#
19+
# 0: A static digit '1'
20+
# 1-5: A random number, left-padded with '0', then reversed
21+
# 6-16: The current number of seconds since epoch, truncated to 10 digits, then reversed
22+
# 17-18: The experiment number
23+
#
24+
# When converted to hex, this creates a pseudorandom 15 digit value. The static
25+
# '1' and number of seconds since epoch are truncated to ensure the value
26+
# remains at 15 digits.
27+
#
28+
# The order of components is very intentional. With the exception of the static
29+
# value at the beginning, the most significant digits are also the most
30+
# variable. This gives the best illusion of "random".
31+
#
32+
# The 'run_id' does not need to be cryptographically secure, only random
33+
# *enough* such that it's extremely unlikely that two identical 'run_id's will
34+
# ever be generated. For a collision to occur, the same experiment number during
35+
# the same second must generate the same random number. It is theoretically
36+
# possible to have another chance for collision in the future since the number
37+
# of seconds since epoch is truncated, but this is only possible once every ~317
38+
# years.
1739
generate_run_id() {
18-
printf '%x' "$(date +%s)"
40+
local time_component rand_component
41+
time_component="$(printf "%.10s" "$(date +%s | rev)")"
42+
rand_component="$(rand=$RANDOM; printf '%05d' "$rand" | rev)"
43+
printf '%x' "1$rand_component$time_component${EXP_NO}"
1944
}
2045

2146
# Init common constants

0 commit comments

Comments
 (0)