Library:Microsoft C Runtime
| Developer | Microsoft |
|---|---|
| Platforms | MS-DOS, Platform:Win16, OS/2 1.x, Win32, Win64 |
| On this wiki | Facts · Code · Notes · Topics |
The C standard library of Microsoft's compilers: Microsoft C for MS-DOS, Win16 and OS/2, then Visual C++ (static LIBC/LIBCMT, msvcrt.dll and msvcr*.dll, later the Universal CRT). Its rand() is a recognizable linear congruential generator.
Overview
The runtime is usually linked statically into games of the 1990s, so its functions show up unnamed in a disassembly. The constants below identify them.
rand()
rand() is a linear congruential generator on a 32-bit state:
state = state * 214013 + 2531011; /* 0x343FD, 0x269EC3 */
return (state >> 16) & 0x7FFF; /* RAND_MAX = 0x7FFF */
srand(seed) sets the state, and the initial state is 1. In the multithreaded runtimes the state is kept per thread instead of in one global.
Identifying it: in 32-bit code the two constants appear as immediates (imul reg, reg, 343FDh followed by add reg, 269EC3h). In 16-bit code the 32-bit multiply goes through a long-multiply helper, so the constants appear split into 16-bit words: 0x0003/0x43FD for the multiplier and 0x0026/0x9EC3 for the increment.
Checked: called through ctypes on Windows 11 (10.0.26200), msvcrt.dll and ucrtbase.dll (Universal CRT, 64-bit) return exactly this sequence: after srand(12345) the first values are 7584, 19164, 25795, and 8 values after srand(12345) and 3 after srand(1) match the formula. The current CRTs still use this generator; the 16-bit runtimes were not run.
Observed in: Software:SkiFree (Win16), where game choices are made with rand() % n (unverified lead, see its notes).
C++ runtime support
The C++ support functions of the runtime (vcruntime140.dll, or linked statically from libvcruntime.lib: _CxxThrowException, __CxxFrameHandler3, __CxxFrameHandler4, __RTDynamicCast, __RTtypeid, type_info) consume the data structures described on Platform:Microsoft Visual C++ ABI: Platform:Microsoft Visual C++ ABI/RTTI and Platform:Microsoft Visual C++ ABI/C++ exception handling.