delorie.com/djgpp/doc/libc/libc_638.html   search  
libc.a reference

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

rand48

Syntax

 
#include <stdlib.h>

double drand48(void);
double erand48(unsigned short state[3]);
unsigned long lrand48(void);
unsigned long nrand48(unsigned short state[3]);
long mrand48(void);
long jrand48(unsigned short state[3]);
void srand48(long seed);
unsigned short *seed48(unsigned short state_seed[3]);
void lcong48(unsigned short param[7]);

Description

This is the family of *rand48 functions. The basis for these functions is the linear congruential formula X[n+1] = (a*X[n] + c) mod 2^48, n >= 0. a = 0x5deece66d and c = 0xb at start and after a call to either srand48 or seed48. A call to lcong48 changes a and c (and the internal state).

drand48 and erand48 return doubles uniformly distributed in the interval [0.0, 1.0).

lrand48 and nrand48 return unsigned longs uniformly distributed in the interval [0, 2^31).

mrand48 and jrand48 return longs uniformly distributed in the interval [-2^31, 2^31).

erand48, jrand48 and nrand48 requires the state of the random generator to be passed.

drand48, lrand48 and mrand48 uses an internal state (common with all three functions) which should be initialized with a call to one of the functions srand48, seed48 or lcong48.

srand48 sets the high order 32 bits to the argument seed. The low order 16 bits are set to the arbitrary value 0x330e.

seed48 sets the internal state according to the argument state_seed (state_seed[0] is least significant). The previous state of the random generator is saved in an internal (static) buffer, to which a pointer is returned.

lcong48 sets the internal state to param[0-2], a to param[3-5] (param[0] and param[3] are least significant) and c to param[6].

Return Value

A random number.

Portability

ANSI/ISO C No
POSIX No

Example

 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{

  srand48(time(NULL));
  printf("%.12f is a random number in [0.0, 1.0).\n", drand48());

  exit(0);
}


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright © 2004     Updated Apr 2004