The *rand command is used to generate a random number within a stated range (e.g. 0-100) and assign that value to a numeric variable.

Note that if you want to take the player to a completely random scene file, there's a command especially for that: *goto_random_scene.


*rand is typically used with a new *temp variable, created as needed, but you can also use *rand on any numeric variable created in startup.txt.

*temp dieroll 0

*rand dieroll 0 7

In the above example we have created a temporary variable called "dieroll" and then assigned to it a random value between (and including) 0 and 7.

We can now use the variable "dieroll" as we would any other numeric variable, typically in an *if statement.

Notes and tips

If you tend to use *rand a lot for a wide range of different purposes, and at different times, it would probably be a good idea to simply declare a single numeric variable for this purpose (e.g. dieroll) in startup.txt and just use that wherever needed -- if only to save you from having to repeatedly create new *temps ad hoc.

N.B. Using excessive randomness in a ChoiceScript game can make it harder to properly test every possibility.

Known *rand-related bug & workaround

There is a minor bug in ChoiceScript where *rand is concerned. Should the player opt to go to the Stats screen while currently on a scene page where *rand had been used, upon exiting from Stats and returning to that scene page, the *rand command will run again, generating a new random value.

Depending on precisely what you've used *rand for, in some cases this probably won't make much difference. If it would -- or if you're simply uncertain -- the workaround to prevent it being a problem is simply to place the *rand command on a separate (i.e. previous) "page" to the one where you actually make use of the value it generates, for example before an earlier *choice or *page_break. Something along the follow lines should suffice:

You carefully open the chest at arm's length, wary of traps . . .

*temp treasure

*rand treasure 50 100

*page_break It creaks open . . .

You gasp in amazement, finding ${treasure} gold coins in the chest!

*set gold + treasure

In the above case, without that vital *page_break, the *rand command would run again (and the displayed value of "treasure" would change) each time the player goes to, and returns from, the Stats screen, whilesoever he or she remains on that particular scene page.

More commands
Choice *choice, *fake_choice, *disable_reuse, *hide_reuse, *allow_reuse, *selectable_if
Variable *create, *temp, *set, Arithmetic operators, *delete, *input_number, *input_text, *print, *rand
Conditional *if, *elseif, *else
Goto *label, *goto, *goto_scene, *goto_random_scene, *gosub, *gosub_scene, *finish
Formatting Bold text, Italic text, *image, *line_break, *page_break, *link, *stat_chart
Miscellaneous *comment, *scene_list, *title, *author, *achieve, *achievement, *check_achievements, *bug, *ending, *more_games, *share_this_game, *show_password, *script

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.