Randomness
Randomness can be used to introduce variety to a visual novel. This can be as simple as varying NPC reactions, or more complex deviations in the path through the story.
Problems with randomness
The main problem with randomness is that the results are random! There's no guarantee that a specific random thing will happen. If parts of your story are locked behind a random roll of the dice the player may never encounter them. This is also a problem during testing.
The random number generator may generate runs of the same result. While there is a technical limit on the length of a run, a run can convince the player that there isn't a random element at all, and a given choice will always end with the same boring result. They then stop exploring the option. This is a particular problem where there is only a small number of possible outcomes.
Randomness doesn't generate the same sense of excitement and anticipation that rolling physical or virtual dice (or drawing cards) does in a table top style game. The player may not even be aware that a "roll" has occurred! This could be alleviated by displaying the rolls made on the player's behalf, and would make sense in a mini-game, or if you are emulating a table-top experience. The flip side is that displaying rolls may lead to added fatigue in playing through the story.
If you are using randomness where the player is in some kind of contest with an NPC it can result in very "swingy" outcomes. This is a particular problem in combat style situations where a series of bad rolls for the player and good rolls for their opponent could result in a death-spiral where the player has to burn limited resources to survive that they need later in the game, or even lead to player death. This increases the cruelty of the game, which is usually a bad thing.
The random number generator is only pseudo-random. Although it appears to generate random values it is actually deterministic, and with enough knowledge future results can be predicted. Don't use it for anything cryptographic!
Python random
and renpy.random
Ren'Py provides a wrapper for the Python random
methods
that supports roll-back.
This means that if you roll-back to an earlier point in the game before
a random roll and then step forward then the same random number will
be generated, leading to the same path through the story.
In general you should be using renpy.random
instead of
the Python ones directly.
Note: If you roll-back and then do a quicksave then a quickload
(or save and load) Ren'Py seems to truncate the roll-back history so
subsequent random numbers will be random again and a different path
will be followed.
Note: Because it is a wrapper, which renpy.random
methods are available is determined by the underlying Python version.
As noted, some of the code presented will only
work with Ren'Py 8 (Python 3.9).
Types of randomness
There are several ways you can generate random picks:
- Dice roll
- The basic way of picking randomly. Some results may never happen during a play through and results may be repeated.
- Draw from a deck
- Results are chosen from a deck of possibilities. All results eventually happen, and none are repeated.
- Picker with LRU
- Results are picked from a list, but recently picked ones are not available to pick from. Some results may never happen, but an effort is made to avoid repeating the same result.