Explicitly set random seed for shuffles

Now the random generator used for shuffling sections (when that is enabled, as
it is by default) a random seed is set explicitly to make sure that shuffles are
predictable. Using the pangamebook filter twice on the same input-file to
generate two different output formats (e.g. PDF and EPUB of the same gamebook)
should now always create two output files with sections in the same order.

It is possible to set the random seed to any number using the gamebook-randomseed
metadata. That should normally not be needed.
This commit is contained in:
Pelle Nilsson 2023-01-20 00:24:00 +01:00
parent 04e46b6290
commit 6db14af9a2
4 changed files with 17 additions and 8 deletions

View file

@ -7,7 +7,7 @@ known use-case is to create a classic [gamebook](https://en.wikipedia.org/wiki/G
Pandoc and Pangamebook should run on most modern computers. It has been tested
on desktop computers running Windows 10, Linux (Lubuntu and Debian) and FreeBSD
13.0. Also on a Raspberry Pi 4 running Raspberry Pi OS and on an Android phone
(in Termux).
(in Termux). Tested versions of Pandoc include 2.9.2.1 and 3.0.
(TBD: Test in OSX.)
@ -98,10 +98,10 @@ Name Type Default Description
gamebook-numbers boolean true Replace section names with numbers
gamebook-post-link string '' Text to add after every link
gamebook-pre-link string '' Text to add before every link
gamebook-randomseed integer 2023 Set random seed for shuffle
gamebook-shuffle boolean true Shuffle sections
gamebook-strong-links boolean true Use strong text style for links
# Gamebook Graph (Graphviz DOT)
The Pandoc filter *pangamebookdot.lua* is included to create a plain-text
DOT file from a generated gamebook, that can then be used with the *dot*
@ -186,7 +186,7 @@ mirror that is updated with new releases.
# LICENSE
MIT License
Copyright (c) 2021-2022 Pelle Nilsson
Copyright (c) 2021-2023 Pelle Nilsson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View file

@ -1,10 +1,10 @@
-- pandoc filter to turn headers and links into numbers
-- Copyright 2021-2022 Pelle Nilsson
-- Copyright 2021-2023 Pelle Nilsson
-- MIT License
-- source: https://github.com/lifelike/pangamebook
-- version: 1.4.0 (2022-08-13)
-- fossil hash: 26a5a109f73c864b55ff771891a8156d8167e0e3d1fbcb2f9a537a4a34bc3364
-- version: 1.5.0 (2023-01-20)
-- fossil hash: f223851d094c9358feefd5c0a5a74506c98fdb14873dfab5afc23664a326ea87
local nr = 1
local mapped = {}
@ -145,6 +145,15 @@ function Pandoc(doc)
link_post = from_meta_string(doc.meta, "gamebook-post-link", "")
if from_meta_bool(doc.meta, "gamebook-shuffle", true) then
local seed_number = 2023
local metadata_seed = doc.meta["gamebook-randomseed"]
if metadata_seed ~= nil then
local metadata_seed_number = tonumber(seed)
if metadta_seed_number ~= nil then
seed_number = metadta_seed_number
end
end
math.randomseed(seed_number)
return pandoc.Pandoc(shuffle_blocks(doc), doc.meta)
else
return doc

View file

@ -3,8 +3,8 @@
-- MIT License
-- source: https://github.com/lifelike/pangamebook
-- version: 1.4.0 (2022-08-13)
-- fossil hash: 26a5a109f73c864b55ff771891a8156d8167e0e3d1fbcb2f9a537a4a34bc3364
-- version: 1.5.0 (2023-01-20)
-- fossil hash: f223851d094c9358feefd5c0a5a74506c98fdb14873dfab5afc23664a326ea87
function one_string_from_block(b)
local result = ""