srand48

srand48

srand48 is a function from the C programming language that initializes a random number generator with a seed value. It belongs to the rand48 family and produces uniformly distributed pseudorandom numbers based on a 48-bit algorithm.

srand48 is a function in the C programming language. It sets the starting value — the so-called seed — of a random number generator. A random number generator is an algorithm, meaning a computational procedure, that produces seemingly random numbers. The word “seemingly” is important: the numbers look random, but are actually calculated according to a fixed mathematical formula. That’s why they are called pseudorandom numbers. srand48 belongs to the so-called rand48 family, a group of related functions that all use the same internal computation method.

The seed: Why the starting value decides everything

Because the algorithm is deterministic — meaning it always produces the same output given the same input — the same seed always generates the same sequence of numbers. This sounds like a flaw, but it is often an advantage. In science, for example, one wants to be able to reproduce experiments: whoever sets the same seed gets the same “random” data and can exactly reproduce a result.

When genuine unpredictability is required, the current time is often used as the seed. It changes every second, so the generator starts at a different point each time. A common beginner’s mistake: calling srand48 inside a loop instead of setting it once at the start of the program. Then the generator keeps restarting — and the randomness is lost.

The 48-bit algorithm behind it

The name “rand48” refers to its internal workings. The generator maintains a state of 48 bits — that is a 48-digit binary number, consisting of zeros and ones. On each call, this state is transformed according to a fixed formula, and the next random number is derived from the result. This method is called a linear congruential generator. It is fast and requires little memory, but is considered too weak for applications where security matters.

srand48 itself does not generate a random number — it only prepares the generator. The actual numbers are provided by sibling functions: drand48 returns a decimal number between 0 and 1, lrand48 returns an integer. srand48 is thus the switch you flip before the machine runs.

48 bits mean that the internal state can take on around 281 trillion different values. That sounds enormous, but it is not enough for cryptographic purposes — that is, for anything related to encryption or security. An attacker who knows the method can, given enough known outputs, reconstruct the seed and predict all subsequent numbers.

Where srand48 appears in practice

srand48 originates from the POSIX standard, a standard for Unix-like operating systems such as Linux or macOS. There, it is part of the standard library and thus available in nearly every C program running on such systems. On Windows, the function is generally missing; other equivalents exist there instead.

In practice, srand48 is mainly encountered in scientific simulations, game development, and statistics. Anyone writing a Monte Carlo simulation, for instance — a method in which approximate values are calculated through many random experiments — uses such generators extensively. Speed matters more than cryptographic strength here, which is why rand48 is a popular choice.

In modern programs, srand48 is encountered less often than in the past. Newer standards like C++11 offer an entire library of generators with clearly defined properties. For security-critical applications — passwords, tokens, encryption keys — functions like getrandom on Linux or CryptGenRandom on Windows are the right choice. srand48 has no place there.

Subscribe free. Unsubscribe the second it sucks.

High-signal news across AI, business, UX, and tech. Every morning.