How To Use Random Numbers & Randomness In Interactive Stories

This article is for authors of interactive adult stories who want to add Random elements to their stories to create more realistic worlds , as well as increasing the “replay value” of stories.

In the real world, life is rarely 100% predictable. None of us choose where or to whom we were born, our appearance, or inborn traits. In a non-interactive story, the author creates the entire history of the characters - from ancestry to birth date, height, personality, health, skills, and life path. In an interactive story, the author has the power to randomize any of a character’s traits so that each read-through of the story is different, as each human life is different. Randomness allows your characters to be “born” with different traits (height, strength, charisma, hair color, etc.) and learn or find different skills or items each time your story is read.

Before starting on Randomness, you should be familiar with the basics of writing interactive stories here at Literotica. Make sure you’ve read our “Getting Started With Writing Adult Interactive Stories”, “How To Write Interactive Stories With Choices”, “How To Use Conditionals in Interactive Fiction”, and “How To Use Variables in Interactive Fiction” articles. Then, let’s find out more about Random Numbers and Randomness!

There are two main ways to add Randomness to an Interactive Story on Literotica: Shuffling Lists and generating Random Numbers.

Shuffling a List to get a Random Result

The easiest way to add Randomness to your story is simply to make a list of items and then “shuffle” the list to show the reader a random item from the list.

Let’s take a look at a simple example of shuffling lists. What if we want to create a character with random attributes? We could do something like this:

VAR hair_color=""
VAR facial_hair=""
VAR weapon=""
VAR shoes=""

You are a mighty wizard.
-> cast_spell_to_conjur_character

=== cast_spell_to_conjur_character
What spell do you want to cast?
+ Conjure a new character.
~hair_color = "{~blue|green}"
~facial_hair = "{~beard|mustache}"
~weapon = "{~rock|stick|machine gun|samurai sword}"
~shoes = "{~sandals|boots|sneakers|fuzzy slippers}"
-> spell_cast

=== spell_cast
Your new friend appears with {hair_color} hair including a bushy {facial_hair}, carrying a {weapon}, and wearing only {shoes}.
Are you ready to go to battle, or do you want to try again?
+ Try again.
-> cast_spell_to_conjur_character
+ Go to battle!
You and your new friend win the battle.
-> END

In the above example, we have a wizard cast a spell to create a new character to join them in battle. We declare our Variables at the start of the story. Then we create the character attributes randomly with shuffled lists when the reader clicks on the Choice “Conjure a new character”:

+ Conjure a new character.
~hair_color = "{~blue|green}"
~facial_hair = "{~beard|mustache}"
~weapon = "{~rock|stick|machine gun|samurai sword}"
~shoes = "{~sandals|boots|sneakers|fuzzy slippers}"
-> spell_cast

Each item in a list is separated by the pipe symbol “|”. Lists “item_1|item_2|item_3” must be enclosed in curly brackets “{item_1|item_2|item_3}” so the script knows it’s not plain text. Add the tilde symbol “~” before the list to tell the script you want it to select a random item from the list “{~item_1|item_2|item_3}”. (Without the tilde symbol, the script will select list items in order left to right rather than randomly.) The list is “shuffled” by the script, and one list item is randomly chosen to be used by the author or shown to the reader. In the above example, we use the randomly selected item to set the value of a Variable.

After assigning values to the Variables using shuffled lists, we show the reader the results of the randomness here:

Your new friend appears with {hair_color} hair including a bushy {facial_hair}, carrying a {weapon}, and wearing only {shoes}.

We then allow the reader to keep the character they created or to cast the spell again and get a new random character. If they keep the character, the author can use the randomly generated traits (Variables) throughout the story, making each read through unique.

Here’s a another simple example of how a shuffled list can be used when an author wants a random result in a story:

Flip a coin. {~heads|tails}

In this example, there’s a list of only two possible outcomes for a coin flip. The two-item list is formatted like this “heads|tails”. If it was an eight-sided dice, the list would be “1|2|3|4|5|6|7|8”.

Roll a dice. {~1|2|3|4|5|6|7|8}

You can add Conditionals, Diverts, and Text to a shuffled list when using it in a Knot. Here’s an example:

VAR coin="none"
Go to the game!
-> heads_tails_game_random

=== heads_tails_game_random
{I flip the coin.|{~Heads {coin=="heads": -> you_win}|Tails!{coin=="tails": -> you_win} You didn't win. -> END}}
Heads or Tails?
+ Heads
~ coin="heads"
-> heads_tails_game_random
+ Tails
~ coin="tails"
-> heads_tails_game_random

=== you_win
You win!
-> END

Most of the above should make sense to you if you’ve read our previous tutorials, but this line is new:

{I flip the coin.|{~Heads {coin=="heads": -> you_win}|Tails!{coin=="tails": -> you_win} You didn't win. -> END}}

The above line contains two nested lists (officially referred to in Ink as “Alternatives”). The first list is shown without shuffling, but the second nested list uses shuffling to randomly pick heads or tails. Here is an explanation of the code:

{
Start the first list.

I flip the coin.
Show this text to the reader only the first time they visit the Knot. There are only two items in the first list, this text and the second (nested) list. So, the first time the reader visits this Knot, they see this text. The next time they visit this Knot (after selecting “Heads” or “Tails”) they see the second item in the first list, which is the nested list.

|
This is a list separator. The item after this will be shown to the reader only on the second time they visit the Knot (in our case, this means after they make a Choice of “Heads” or “Tails”).

{
Start the second (nested) list.

~Heads {coin=="heads": -> you_win}
The “~” symbol at the start of a list tells the script that this list will be shuffled (one random item from the list will be shown to the reader). If this item in the list is selected, the text “Heads” will be shown to the reader. The script will then check to see whether the reader selected “Heads” by checking to see if the value of the Variable “coin” is equal to “heads”. If the Variable matches “heads” then the Conditional sends the reader to the “you_win” Knot. If the value of the Variable does not match “heads” then the script continues to the end of the nested list where the “You didn't win. -> END” tells the reader that they didn’t win and ends the story.

|
This is a list separator. This list is shuffled, so each item has an equal chance of being shown to the reader.

Tails!{coin=="tails": -> you_win}
This is the second item in the shuffled list. The “Tails” list item works exactly the same as the “Heads” one above. If the reader selected “Tails” then it will match the Variable and the Conditional will send them to the “you_win” Knot.

You didn't win. -> END
If the reader is not redirected by one of the Conditional statements, it means that the Variable was not a match and we show them the “You didn’t win.” text and end the story.

}
End the second (shuffled) list.

}
End the first list.

That explains how we’re able to do a coin flip using a shuffled list to get a random result. While the example is done in a single Knot, the same action in a story could be split up into several different Knots if you prefer to separate the logic more.

Shuffled lists are a great option when you have a finite list of items that you want to interact with your characters or the world in a random way. If you’re working on a story that uses a lot of numbers rather than list items, there’s a better way to generate Random Numbers.

What is a Random Number and How Can They Be Used?

A Random Number is just a number that you and the reader can’t predict in advance. For example, when you roll a dice, you don’t know what number it will land on. In a story, you could use a Random Number to set your main character’s starting “charisma points”, and then require the character to go on a quest to improve their charisma before they can find romance. Within that quest, you could use Random Numbers to vary the number of new charisma points the character earns or loses from each interaction with another character.

Generating Random Numbers in Literotica Interactive Stories is simple:

{RANDOM(1, 400)}

You can likely guess how that works by looking at it. The output will be a random number between 1 and 400. The starting and ending numbers are included in the range, so both “1” and “400” are possible outcomes, as well as any number in between. Here’s another example of creating a unique character, this time using Random Numbers:

VAR strength=""
VAR stamina=""
VAR weight=""
VAR hit_points=""
VAR charisma=""

You are a mighty wizard.

-> cast_spell_to_conjur_character

=== cast_spell_to_conjur_character
What spell do you want to cast?
+ Conjure a new character.
~strength = "{RANDOM(200, 400)}"
~stamina = "{RANDOM(400, 800)}"
~weight = "{RANDOM(100, 250)}"
~hit_points = "{RANDOM(1000, 7000)}"
~charisma = "{RANDOM(100, 700)}"
-> spell_cast

=== spell_cast
It took over {RANDOM(2, 4)} {~Hours|Days} but your spell worked!
Your new friend has {strength} strength, {stamina} stamina, weighs in at {weight} pounds, has {hit_points} hit points, and {charisma} charisma.
Are you ready to go to battle, or do you want to try again?
+ Try again.
-> cast_spell_to_conjur_character
+ Go to battle!
You and your new friend win the battle.
-> END

The random output of the above example is:

It took over {RANDOM(2, 4)} {~Hours|Days} but your spell worked!

We added Random Numbers to the above line to make spell casting more fun. These number have no impact on the game beyond the reader reading them in this Knot. If we were keeping track of time in our story, we could create a Variable to know how much time passed during the spell casting (but we aren’t doing that in the code above).

Your new friend has {strength} strength, {stamina} stamina, weighs in at {weight} pounds, has {hit_points} hit points, and {charisma} charisma.

The above line shows the reader the random character stats we generated when they clicked on the Choice “cast_spell_to_conjur_character”. These stats are stored in Variables we declared at the start of the story, so they can be used and changed throughout the story, creating a unique reading experience for every reader.

Another obvious use for Random Numbers is to update the Variables in your story based on Choices that the reader makes. Fixed numbers can be used to update stats and other Variables, but Random Numbers potentially make the story more dynamic. Here is a simple example where a character’s charisma is updated based on the reader’s Choice:

=== give_gift_to_wife
It's your anniversary, what do you do?
+ Go on an expensive vacation with my wife!
~charisma = charisma + "{RANDOM(200, 400)}"
-> day_after_anniversary_knot
+ Buy my wife a nice gift for our anniversary!
~charisma = charisma + "{RANDOM(100, 200)}"
-> day_after_anniversary_knot
+ Oops, I forgot it was our anniversary!
~charisma = charisma + "{RANDOM(-200, -100)}"
-> day_after_anniversary_knot
+ Visit my girlfriend's house!
~charisma = charisma + "{RANDOM(-800, -400)}"
-> day_after_anniversary_knot

==day_after_anniversary_knot
Your charisma is now {charisma}!
-> END

You should be able to tell how we update the “charisma” Variable above, and how we then display it back to the reader in the next Knot. As far as using this is a real story, we could have some part of the story that requires a minimum charisma number before the reader can proceed further, with side quests that have the potential to both increase and decrease the reader’s charisma score.

The uses for Random Numbers and Shuffled Lists in interactive stories are nearly limitless, unlocking narrative pathways that wouldn’t be possible in other storytelling formats.

Further Reading on Advanced Random Number and Shuffled List Usage

The creators of the Ink language have written a detailed help document explaining how to use every advanced feature of the language. If you’re interested in becoming a power user, please see their “Writing With Ink” tutorial for more information. We also have our own growing library of Interactive Story Writing Tutorials and FAQs that you might want to read.

If you’re interested in helping test and give feedback on the new Literotica Interactive Story format, either as an author or a reader, please read this thread in the Literotica Forum: Interactive Adult Story Testers Needed.