Skip to content

Commit

Permalink
more robust (?) fix for the broken PRNG
Browse files Browse the repository at this point in the history
I add the PID of the current script to the seed

I also replaced the "while true" loop to "for _ in $(seq 100)" to
be able to detect the problem and skip the mission in that case.
  • Loading branch information
phyver committed Jul 9, 2021
1 parent 2d95c92 commit abd546c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 9 deletions.
3 changes: 3 additions & 0 deletions missions/basic/12_cp_ls_mtime_paintings_tower/i18n/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ msgstr "$MISSION_DIR/goal/en.txt"
msgid "$MISSION_DIR/treasure-msg/en.txt"
msgstr "$MISSION_DIR/treasure-msg/en.txt"

msgid "Problem with generation of random filename!"
msgstr "Problem with generation of random filename!"

msgid "The painting in your chest is invalid..."
msgstr "The painting in your chest is invalid..."

Expand Down
3 changes: 3 additions & 0 deletions missions/basic/12_cp_ls_mtime_paintings_tower/i18n/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ msgstr "$MISSION_DIR/goal/fr.txt"
msgid "$MISSION_DIR/treasure-msg/en.txt"
msgstr "$MISSION_DIR/treasure-msg/fr.txt"

msgid "Problem with generation of random filename!"
msgstr "Problème lors de la génération d'un nom de fichier aléatoire !"

msgid "The painting in your chest is invalid..."
msgstr "Le tableau de votre coffre est invalide..."

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ msgstr ""
msgid "$MISSION_DIR/treasure-msg/en.txt"
msgstr ""

msgid "Problem with generation of random filename!"
msgstr ""

msgid "The painting in your chest is invalid..."
msgstr ""

Expand Down
21 changes: 18 additions & 3 deletions missions/basic/12_cp_ls_mtime_paintings_tower/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ _mission_init() (
i=$(RANDOM)
i=$((1+i%3))

while true
for _ in $(seq 100)
do
filename="$(gettext "painting")_$(random_string 8)"
[ -e "$filename" ] || break
done
if [ -e "$filename" ]
then
echo "$(gettext "Problem with generation of random filename!")" >&2
return 1
fi
box.sh -B Diamond "$MISSION_DIR/ascii-art/painting-$i" > "$filename"
Y=$((1980 + $(RANDOM)%10))
M=$(printf "%02d" $((1 + $(RANDOM)%12)))
Expand All @@ -28,11 +33,16 @@ _mission_init() (
touch -t "$Y$M$D$h$m.$s" "$filename"

i=$((1+i%3))
while true
for _ in $(seq 100)
do
filename="$(gettext "painting")_$(random_string 8)"
[ -e "$filename" ] || break
done
if [ -e "$filename" ]
then
echo "$(gettext "Problem with generation of random filename!")" >&2
return 1
fi
box.sh -B Diamond "$MISSION_DIR/ascii-art/painting-$i" > "$filename"
Y=$((1995 + $(RANDOM)%10))
M=$(printf "%02d" $((1 + $(RANDOM)%12)))
Expand All @@ -44,11 +54,16 @@ _mission_init() (
touch -t "$Y$M$D$h$m.$s" "$filename"

i=$((1+i%3))
while true
for _ in $(seq 100)
do
filename="$(gettext "painting")_$(random_string 8)"
[ -e "$filename" ] || break
done
if [ -e "$filename" ]
then
echo "$(gettext "Problem with generation of random filename!")" >&2
return 1
fi
box.sh -B Diamond "$MISSION_DIR/ascii-art/painting-$i" > "$filename"
Y=$((2010 + $(RANDOM)%10))
M=$(printf "%02d" $((1 + $(RANDOM)%12)))
Expand Down
4 changes: 2 additions & 2 deletions scripts/RANDOM
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ GSH_CONFIG=$GSH_ROOT/.config

n=${1:-1}

awk -v seed_file="$GSH_CONFIG/PRNG_seed" -v n="$n" 'BEGIN {
awk -v seed_file="$GSH_CONFIG/PRNG_seed" -v n="$n" -v PID=$$ 'BEGIN {
getline seed < seed_file;
if (seed) srand(seed); else srand();
for (i=0; i<n; i++) {
print int(rand()*65536);
}
if (seed) printf("%d\n", int(2^31 * rand())) > seed_file;
if (seed) printf("%d\n", int(2^30 * rand())+PID) > seed_file;
}'
4 changes: 2 additions & 2 deletions scripts/random_string
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ GSH_ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
GSH_CONFIG=$GSH_ROOT/.config
n=$1

awk -v n=${n:-32} -v seed_file="$GSH_CONFIG/PRNG_seed" 'BEGIN {
awk -v n=${n:-32} -v seed_file="$GSH_CONFIG/PRNG_seed" -v PID=$$ 'BEGIN {
getline seed < seed_file;
if (seed) srand(seed); else srand();
l = split("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z", chars);
for(i=0;i<n;i++) {
printf("%c", chars[1+int(rand()*l)]);
}
printf("\n");
if (seed) printf("%d\n", int(2^31 * rand())) > seed_file;
if (seed) printf("%d\n", int(2^30 * rand())+PID) > seed_file;
}'
4 changes: 2 additions & 2 deletions scripts/treasure_message
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ display_message() {
echo
"$GSH_ROOT/scripts/reflow.awk" -v width=$reflow_width "$treasure_msg_file" |
paste "$GSH_LIB/ascii-art/treasure-chest.txt" - |
awk -v width=$WIDTH -v seed_file="$GSH_CONFIG/PRNG_seed" '
awk -v width=$WIDTH -v seed_file="$GSH_CONFIG/PRNG_seed" -v PID=$$ '
BEGIN{
getline seed < seed_file;
if (seed) srand(seed); else srand();
Expand All @@ -43,7 +43,7 @@ BEGIN{
print s "" $0;
}
END {
if (seed) printf("%d\n", int(2^31 * rand())) > seed_file;
if (seed) printf("%d\n", int(2^30 * rand())+PID) > seed_file;
}
/^[^\t]/ { print $0; }
' | column -t -s" "
Expand Down

0 comments on commit abd546c

Please sign in to comment.