GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
lrand48.c File Reference

GIS Library - Pseudo-random number generation. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <grass/gis.h>
#include <grass/glocale.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
Include dependency graph for lrand48.c:

Go to the source code of this file.

Macros

#define LRAND48_ATOMIC   0
 
#define LO(x)   ((x) & 0xFFFFU)
 
#define HI(x)   ((x) >> 16)
 

Typedefs

typedef unsigned short uint16
 
typedef unsigned int uint32
 
typedef signed int int32
 

Functions

void G_srand48 (long seedval)
 Seed the pseudo-random number generator.
 
long G_srand48_auto (void)
 Seed the pseudo-random number generator from the time and PID.
 
long G_lrand48 (void)
 Generate an integer in the range [0, 2^31)
 
long G_mrand48 (void)
 Generate an integer in the range [-2^31, 2^31)
 
double G_drand48 (void)
 Generate a floating-point value in the range [0,1)
 

Detailed Description

GIS Library - Pseudo-random number generation.

The generator is the standard drand48 linear congruential generator X' = (A * X + B) mod 2^48 with A = 0x5DEECE66D and B = 0xB.

When C11 atomic operations are available, the generator state is advanced with an atomic compare-and-swap and the generating functions are thread-safe: the sequence of generated values for a given seed is the same as in a single-threaded run. Which thread receives which value depends on scheduling, so results are fully reproducible only with single-threaded execution. Without C11 atomics (notably MSVC, which defines STDC_NO_ATOMICS), the generator falls back to plain state updates, so multi-threaded usage is safe only when compiled with C11 atomics.

The seeding functions are not thread-safe; see G_srand48().

SPDX-FileCopyrightText: 2014-2026 GRASS Development Team SPDX-License-Identifier: GPL-2.0-or-later

Authors
Glynn Clements, Maris Nartiss (thread safety)

Definition in file lrand48.c.

Macro Definition Documentation

◆ HI

#define HI (   x)    ((x) >> 16)

Definition at line 84 of file lrand48.c.

◆ LO

#define LO (   x)    ((x) & 0xFFFFU)

Definition at line 83 of file lrand48.c.

◆ LRAND48_ATOMIC

#define LRAND48_ATOMIC   0

Definition at line 38 of file lrand48.c.

Typedef Documentation

◆ int32

Definition at line 55 of file lrand48.c.

◆ uint16

Definition at line 53 of file lrand48.c.

◆ uint32

Definition at line 54 of file lrand48.c.

Function Documentation

◆ G_drand48()

double G_drand48 ( void  )

Generate a floating-point value in the range [0,1)

This function is thread-safe only when compiled with C11 atomics (see the comment at the top of the file).

Returns
the generated value

Definition at line 256 of file lrand48.c.

References r.

Referenced by f_rand(), and G_math_rand().

◆ G_lrand48()

long G_lrand48 ( void  )

Generate an integer in the range [0, 2^31)

This function is thread-safe only when compiled with C11 atomics (see the comment at the top of the file).

Returns
the generated value

Definition at line 212 of file lrand48.c.

References r.

Referenced by Rast_make_random_colors().

◆ G_mrand48()

long G_mrand48 ( void  )

Generate an integer in the range [-2^31, 2^31)

This function is thread-safe only when compiled with C11 atomics (see the comment at the top of the file).

Returns
the generated value

Definition at line 233 of file lrand48.c.

References r.

Referenced by f_rand().

◆ G_srand48()

void G_srand48 ( long  seedval)

Seed the pseudo-random number generator.

This function is not thread-safe. In a multi-threaded program, call G_srand48() once before starting the worker threads; it must not run concurrently with another thread seeding or generating values.

Parameters
[in]seedval32-bit integer used to seed the PRNG

Definition at line 95 of file lrand48.c.

References HI, LO, state, and x.

Referenced by G_math_srand(), and G_srand48_auto().

◆ G_srand48_auto()

long G_srand48_auto ( void  )

Seed the pseudo-random number generator from the time and PID.

A weak hash of the current time and PID is generated and used to seed the PRNG

This function is not thread-safe. In a multi-threaded program, call G_srand48_auto() once before starting the worker threads; it must not run concurrently with another thread seeding or generating values.

Returns
generated seed value passed to G_srand48()

Definition at line 121 of file lrand48.c.

References _, AMI_STREAM< T >::AMI_STREAM(), G_fatal_error(), G_srand48(), getpid, gettimeofday(), NULL, and t.

Referenced by G_math_srand_auto(), and Rast_make_random_colors().