BRL-CAD

A supply of fast pseudo-random numbers from table in bn/rand.c. The values are in the open interval (i.e. exclusive) of 0.0 to 1.0 range with a period of 4096. More...

Collaboration diagram for Random Number Tables:

Files

file  rand.h
 

Macros

#define BN_RAND_TABSIZE   4096
 
#define BN_RAND_TABMASK   0xfff
 
#define BN_RANDSEED(_i, _seed)   _i = ((unsigned)_seed) % BN_RAND_TABSIZE
 
#define BN_RANDOM(_i)   bn_rand_table[ _i = (_i+1) % BN_RAND_TABSIZE ]
 
#define BN_RANDHALF(_i)   (bn_rand_table[ _i = (_i+1) % BN_RAND_TABSIZE ]-0.5)
 
#define BN_RANDHALF_INIT(_p)   _p = bn_rand_table
 
#define BN_RANDHALFTABSIZE   16535
 
#define bn_rand_half(_p)
 
#define bn_rand_init(_p, _seed)
 
#define bn_rand0to1(_q)   (bn_rand_half(_q)+0.5)
 
#define BN_SINTABSIZE   2048
 
#define bn_tab_sin(_a)
 

Functions

void bn_mathtab_constant (void)
 For benchmarking purposes, make the random number table predictable. Setting to all zeros keeps dithered values at their original values. More...
 
void bn_rand_sph_sample (point_t sample, const point_t center, const fastf_t radius)
 Generate a sample point on a sphere per Marsaglia (1972). More...
 

Variables

const float bn_rand_table [BN_RAND_TABSIZE]
 
int bn_randhalftabsize
 
float bn_rand_halftab [BN_RANDHALFTABSIZE]
 
const float bn_sin_table [BN_SINTABSIZE]
 

Detailed Description

A supply of fast pseudo-random numbers from table in bn/rand.c. The values are in the open interval (i.e. exclusive) of 0.0 to 1.0 range with a period of 4096.

Usage:
unsigned idx;
float f;
BN_RANDSEED(idx, integer_seed);
while (NEED_MORE_RAND_NUMBERS) {
f = BN_RANDOM(idx);
}
#define BN_RANDOM(_i)
Definition: rand.h:78
#define BN_RANDSEED(_i, _seed)
Definition: rand.h:65

Note that the values from bn_rand_half() become all 0.0 when the benchmark flag is set (bn_rand_halftab is set to all 0's). The numbers from bn_rand_table do not change, because the procedural noise would cease to exist.

Macro Definition Documentation

◆ BN_RAND_TABSIZE

#define BN_RAND_TABSIZE   4096

Definition at line 63 of file rand.h.

◆ BN_RAND_TABMASK

#define BN_RAND_TABMASK   0xfff

Definition at line 64 of file rand.h.

◆ BN_RANDSEED

#define BN_RANDSEED (   _i,
  _seed 
)    _i = ((unsigned)_seed) % BN_RAND_TABSIZE

Definition at line 65 of file rand.h.

◆ BN_RANDOM

#define BN_RANDOM (   _i)    bn_rand_table[ _i = (_i+1) % BN_RAND_TABSIZE ]

BN_RANDOM always gives numbers between the open interval 0.0 to 1.0

Definition at line 78 of file rand.h.

◆ BN_RANDHALF

#define BN_RANDHALF (   _i)    (bn_rand_table[ _i = (_i+1) % BN_RAND_TABSIZE ]-0.5)

BN_RANDHALF always gives numbers between the open interval -0.5 and 0.5

Definition at line 81 of file rand.h.

◆ BN_RANDHALF_INIT

#define BN_RANDHALF_INIT (   _p)    _p = bn_rand_table

Definition at line 82 of file rand.h.

◆ BN_RANDHALFTABSIZE

#define BN_RANDHALFTABSIZE   16535

Powers of two give streaking

Definition at line 84 of file rand.h.

◆ bn_rand_half

#define bn_rand_half (   _p)
Value:
(_p) < bn_rand_halftab) ? \
*((_p) = bn_rand_halftab) : *(_p))
float bn_rand_halftab[BN_RANDHALFTABSIZE]
int bn_randhalftabsize

random numbers between the closed interval -0.5 to 0.5 inclusive, except when benchmark flag is set, when this becomes a constant 0.0

Parameters
_pfloat pointer type initialized by bn_rand_init()

Definition at line 102 of file rand.h.

◆ bn_rand_init

#define bn_rand_init (   _p,
  _seed 
)
Value:
(_p) = &bn_rand_halftab[ \
(int)(\
(bn_rand_halftab[(_seed)%bn_randhalftabsize] + 0.5) * \

initialize the seed for the large random number table (halftab)

Parameters
_pfloat pointer to be initialized, used for bn_rand0to1() and bn_rand_half()
_seedInteger SEED for offset in the table.

Definition at line 115 of file rand.h.

◆ bn_rand0to1

#define bn_rand0to1 (   _q)    (bn_rand_half(_q)+0.5)

random numbers in the closed interval 0.0 to 1.0 range (inclusive) except when benchmarking, when this is always 0.5

Parameters
_qfloat pointer type initialized by bn_rand_init()

Definition at line 128 of file rand.h.

◆ BN_SINTABSIZE

#define BN_SINTABSIZE   2048

Definition at line 130 of file rand.h.

◆ bn_tab_sin

#define bn_tab_sin (   _a)
Value:
(((_a) > 0) ? \
(bn_sin_table[(int)((0.5+ (_a)*(BN_SINTABSIZE / M_2PI)))&(BN_SINTABSIZE-1)]) :\
(-bn_sin_table[(int)((0.5- (_a)*(BN_SINTABSIZE / M_2PI)))&(BN_SINTABSIZE-1)]))
const float bn_sin_table[BN_SINTABSIZE]
#define BN_SINTABSIZE
Definition: rand.h:130
#define M_2PI
Definition: vmath.h:164

Definition at line 132 of file rand.h.

Function Documentation

◆ bn_mathtab_constant()

void bn_mathtab_constant ( void  )

For benchmarking purposes, make the random number table predictable. Setting to all zeros keeps dithered values at their original values.

◆ bn_rand_sph_sample()

void bn_rand_sph_sample ( point_t  sample,
const point_t  center,
const fastf_t  radius 
)

Generate a sample point on a sphere per Marsaglia (1972).

Note that bn_sph_sample and its internal routines do not initialize the randmt seed - the user should call bn_randmt_seed in their code if a variable seed is required.

Variable Documentation

◆ bn_rand_table

const float bn_rand_table[BN_RAND_TABSIZE]
extern

This is our table of random numbers. Rather than calling drand48() or random() or rand() we just pick numbers out of this table. This table has 4096 unique entries with floating point values ranging from the open interval (i.e. exclusive) 0.0 to 1.0 range.

There are convenience macros for access in the bn.h header.

◆ bn_randhalftabsize

int bn_randhalftabsize
extern

◆ bn_rand_halftab

float bn_rand_halftab[BN_RANDHALFTABSIZE]
extern

The actual table of random floating point numbers with values in the closed interval (i.e. inclusive) -0.5 to +0.5 range.

For benchmarking purposes, this table is zeroed.

◆ bn_sin_table

const float bn_sin_table[BN_SINTABSIZE]
extern

table of floating point sine values in the closed (i.e. inclusive) interval -1.0 to 1.0 range.