BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

Random numbers: who draws, from which generator, in which order

BF22142Heroes

A port that must behave like the original cannot use its own dice. Where the original draws a random number, the port must draw the same number, which means the same generator, the same starting state, and the same draws before it. This page lists the generators the three games use, who shares which, and where the port stands with each.

Who draws from which generator the shared Random one object a process, never reset spawners' delays an empty seat's input armor water checks collision effects engine sound pitch projectile lifetimes bots' choices generators of their own seeded so that two machines agree deviation: a table by action number ricochet: seeded by the tick penetration: seeded by the tick particles: one in the renderer artillery, ambient areas the C runtime's rand() one state for the whole process animation idles, head turns a radio line's variant an explosion's push on a soldier a vehicle drop's offset a sound's pitch variant bf2_w32ded.exe!0x00580750 (Random::next); the state starts at 0x759dc150
The streams. A box is one generator's state; everything under it draws from it in turn.

The engine's generator#

Nearly all random numbers come from one small class, Random (Random::next, bf2_w32ded.exe!0x00580750):

text
state = state x 24112  mod (2^31 - 1)
state = state x 26143  mod (2^31 - 1)
k     = ((state | 0x80) >> 7) + 1            a value from 1 to 2^24
unit  = k x 2^-24                            in (0, 1]
signed = k x 2^-23 - 1                       in (-1, 1]

A fresh object starts at 0x759dc150. A state of 0 never moves: every draw from it is the same.

On top of it sits a distribution sampler (RandomDistribution, 0x00833e70 is the object most of them draw from): a template property such as a projectile's timeToLive or an effect's randomAgeFactor names a kind (none, uniform, and others), a mean and a spread, and a sample is one or more draws.

One object a process, shared by strangers#

Each game has one Random object that unrelated systems share (Battlefield 2's server keeps it at 0x00833e70, Heroes 1.126 at 0x01313cf4). It exists from the process's start, starts at 0x759dc150, and is written only by its own draws: no level change resets it. Who draws from it, as the notes' audit lists for the server:

WhoWhen
a vehicle spawnerits delay, at each spawn (spawners and wrecks)
an empty seat that makes random inputfive draws a refresh
an armor's water checkthe time of the next check
a collision responseone draw per effect slot it fills
an engine's soundtwo draws when its pitch is redrawn
a projectileits time to live, at the launch, a ricochet, a penetration
a botits kit's unlocks; a random point to look at
effectsambient effects, effect bundles, emitters, on the client

Because they share one stream, the order of draws is part of the game's state. If a spawner draws before an armor in one tick and after it in the next build, both get other numbers, though neither changed.

The port's stream differs across objects, and says so

The original walks one map of objects in id order each tick, so one vehicle's armor, seats and engine draw before the next vehicle's. The port updates by kind: all spawners, then all armors, then vehicle parts. The draws are the same; their order across objects is not, so the same object gets another value than on the original. The notes file this as open: the mend is one update pass over all kinds of object. For Battlefield 2's bots the number of draws per tick was matched and pinned in the tests.

Generators of their own#

Some systems have a Random object to themselves, so their numbers do not depend on anyone else:

WhoIts generatorSeeded
weapon deviationa table of 1,024 draws made once (deviation)the table from a fixed seed; the index from the player's action number
ricochet and penetrationone static generator eachset to tick mod 1023 before every use: the outcome depends on the tick alone (projectiles)
particle systemsone in the renderer, shared by all systems0x759dc150, never reseeded (effects)
effect bundlesone in the client0x759dc150
artillery, ambient areas, the spawn points' scatterone eachtheir own

The first two are the clever ones. A shot's spread must be the same on the shooter's client and on the server, and they cannot share a stream; so the spread is a pure function of something both already have: the player's action number, or the tick.

The C runtime's rand()#

A few places call the C library's rand(), a different generator with one state for the whole process:

  • the animation system's idle triggers and the head's idle turn (animation);
  • the variant of a radio line (radio);
  • the push an explosion gives a soldier (damage and armor);
  • the offset of a vehicle drop, and a sound sample's pitch and volume variants.

This stream is shared with everything else in the process that calls rand(), including code that is not the game's. The port keeps its own copy of the C runtime's generator with a named site for each draw, and its tests compare the stream with recorded sessions of the original: 68 of 68 draws equal in site and state in one run, 696 of 696 on a ladder, 174 of 177 on a zipline (the rest fall on the respawn tick).

What differs per game#

BF2
Battlefield 2

The spawner's delay multiplies the random share by a constant zero: the draw's result is thrown away and every respawn waits the minimum.

2142
Battlefield 2142 and Heroes

In 2142 and Heroes the spawner's delay does use the draw: a delay between the minimum and the maximum. Of the level spawner templates, 1,360 of 2142's 2,539 and 412 of Heroes' 617 have a range.

This is where the port had a fault of exactly the kind this page is about. Its generator lived in the world object, which is replaced at each level change, and held 0 until the first tick. A spawner that draws while the level loads met the state 0 and always got the same value: the maximum. The port now starts the generator before the level's objects are made and hands its state on at an unload, as the original's never-reset object behaves; a test makes two spawners at a level load and checks they take two different delays.

Heroes has a second object for one purpose (where a projectile's damage lies between its template's two values) and draws from the shared one for its rocket pack's direction, a struck soldier's knockback and a flag's first check interval.

How the port keeps the streams#

StreamState
the generator itself, its derived values, the samplerported; the same numbers
the shared object: started at the process's start, kept across levelsported for all three games, with tests
which sites draw from itaudited against each binary's references to the object: read, not run draw for draw for 2142 and Heroes (no tracer reads their generator yet)
the order of draws across objects in a tickdiffers, filed as open (the note above)
ricochet, penetration, deviationverified, the same bits (projectiles)
the particle generator and the order of its drawsverified against dumped runs of the renderer
the C runtime's streamcompared with recorded sessions of the original server, draw by draw

How it is tested#

  • The generator itself (crates/bf2-gameplay/src/random.rs): ten thousand steps against the plain modular product, and two values worked out by hand in the test's comment (your first hour redoes them).
  • The one shared object starts at its static value (crates/bf2-server/src/bots.rs), and test programs in a 2142 and in a Heroes process hold which sites draw from it over a level's life (global_random_2142.rs, global_random_heroes.rs).
  • In play: the game rules rig and the differentials only stay equal to the original server while every draw happens in the original's order, so a missing or extra draw shows as a round that diverges (test rigs).

In the code#

WhatWhere
the generatoreach system carries its copy: crates/bf2-ai/src/random.rs, crates/bf2-effects/src/rng.rs, the server's in crates/bf2-world
the server's shared object and its life across levelscrates/bf2-server/src/bots.rs (global_random_start, global_random_keep)
the C runtime's generator with named sitescrates/bf2-core (crt), crates/bf2-anim (crt)
the tests of the shared streamcrates/bf2-server/tests/it_2142/global_random_2142.rs, global_random_heroes.rs

Notes: notes/subsystems/global-random.md, notes/subsystems/animation.md (section 11, "CRT"), notes/subsystems/effects.md (section 4.1).

Source: docs/dev/engine/random-numbers.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.