On this page:
%random
%random-number
8.18.0.13

3.6 Random Code🔗ℹ

Can be used just about anywhere to add an element of randomness to your script.

syntax

(%random random-clause ...)

 
random-clause = [percent-chance then-body ...]
Game versions: All

Arguments:

  • percent-chance - integer (0-99)

  • then-body - anything

Specify a piece of code that has a defined chance of being chosen.

  • If the total percentages add up to less than 99%, there is a chance that none of them get chosen.

  • If the total exceeds 99%, only the first 99% will have a chance of occurring.

  • Random constructs can encompass individual arguments, or even whole blocks of code.

  • They cannot be nested. To achieve a non-integer chance, use a first random block to define (using %define) which additional random block to run.

  • BUG: if the first branch is 0, it is still chosen occasionally. Do not include a 0 chance branch as the first branch. Also note that the 100th percent is never chosen.

  • BUG (AoC/HD/UP): Comments in dead branches are not ignored. Do not include any underlying random syntax (ie. end_random) in such comments. For more information, see this external article: Parser Pitfalls

Example: Place 5 or 6 or 7 gold mines with 6 being the most likely.
#lang aoe2-rms
 
<OBJECTS-GENERATION>
(create-object 'GOLD
               (%random [30 (number-of-objects 5)]
                        [50 (number-of-objects 6)]
                        [20 (number-of-objects 7)]))

Example: Have a 10% chance of placing 5 gold mines (and a 90% of not doing so)
#lang aoe2-rms
 
<OBJECTS-GENERATION>
(%random [10 (create-object 'GOLD
                            (number-of-objects 5))])

procedure

(%random-number min max)  string?

  min : any/c
  max : any/c
Game versions: UP/DE Arguments: min - number max - number

Randomize a numeric argument between min and max (inclusive).

Example: Place 5 or 6 or 7 gold piles, and randomly change the amount of gold (it will be the same amount in all of them)
#lang aoe2-rms
 
<OBJECTS-GENERATION>
(create-object 'GOLD
               (number-of-objects (%random-number 5 7))
               (resource-delta (%random-number -200 300)))