File: random.c

package info (click to toggle)
fitsh 0.9.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,024 kB
  • sloc: ansic: 56,150; makefile: 1,147; sh: 806
file content (88 lines) | stat: -rw-r--r-- 2,207 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*****************************************************************************/
/* random.c								     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* (C) 2011; Pal, A. (apal@szofi.net); based on the code available in 	     */
/* the book: Numerical Recipes: the art of scientific computing (3rd ed.)    */
/*****************************************************************************/

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

#include <random/random.h>

static struct	random_state	state;
static int	is_initialized=0;

#define		R2P32	2.3283064365386962890625E-10

/*****************************************************************************/

int random_state_seed(struct random_state *state,int j)
{
 state->v  = 2244614371U;
 state->w1 =  521288629U;
 state->w2 =  362436069U;
 state->u  =  (uint32_t)j ^ state->v;
 random_state_uint32(state);
 state->v  = state->u;
 random_state_uint32(state);
 return(0);
}

uint32_t random_state_uint32(struct random_state *state)
{
 uint32_t	x,y;

 state->u = state->u * 2891336453U + 1640531513U;
 state->v ^= state->v >> 13;
 state->v ^= state->v << 17;
 state->v ^= state->v >>  5;
 state->w1 = 33378 * (state->w1 & 0xffff) + (state->w1 >> 16);
 state->w2 = 57225 * (state->w2 & 0xffff) + (state->w2 >> 16);
 x = state->u ^ (state->u << 9); 
 x ^= x >> 17; 
 x ^= x <<  6;
 y = state->w1 ^ (state->w1 << 17); 
 y ^= y >> 15; 
 y ^= y << 5;
 return((x+state->v)^(y+state->w2));
}

double random_state_double(struct random_state *state)
{
 uint32_t	r1,r2;
 r1=random_state_uint32(state);
 r2=random_state_uint32(state);
 return(R2P32*((double)r1+R2P32*(double)r2));
}

/*****************************************************************************/

int random_seed(int j)
{
 random_state_seed(&state,j);
 is_initialized=1;
 return(0);
}

uint32_t random_uint32(void)
{ 
 if ( ! is_initialized )
  {	random_seed(0);
	is_initialized=1;
  }
 return(random_state_uint32(&state));
}

double random_double(void)
{
 if ( ! is_initialized )
  {	random_seed(0);
	is_initialized=1;
  }
 return(random_state_double(&state));
}

/*****************************************************************************/