File: my16807.c

package info (click to toggle)
testu01 1.2.3%2Bds1-3
  • links: PTS, VCS
  • area: non-free
  • in suites: sid, trixie
  • size: 17,748 kB
  • sloc: ansic: 52,357; makefile: 248; sh: 53
file content (58 lines) | stat: -rw-r--r-- 1,354 bytes parent folder | download | duplicates (4)
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
#include "my16807.h"
#include <testu01/unif01.h>
#include <testu01/util.h>
#include <testu01/addstr.h>
#include <string.h>

typedef struct { double S; } My16807_state;

static double My16807_U01 (void *par, void *sta)
{
   My16807_state *state = sta;
   long k;
   state->S *= 16807.0;
   k = state->S / 2147483647.0;
   state->S -= k * 2147483647.0;
   return (state->S * 4.656612875245797E-10);
}

static unsigned long My16807_Bits (void *par, void *sta)
{
   return (unsigned long) (My16807_U01 (par, sta) * 4294967296.0);
}

static void WrMy16807 (void *sta)
{
   My16807_state *state = sta;
   printf (" S = %.0f\n", state->S);
}

unif01_Gen *CreateMy16807 (int s)
{
   unif01_Gen *gen;
   My16807_state *state;
   size_t leng;
   char name[60];

   gen = util_Malloc (sizeof (unif01_Gen));
   gen->state = state = util_Malloc (sizeof (My16807_state));
   state->S = s;
   gen->param = NULL;
   gen->Write = WrMy16807;
   gen->GetU01 = My16807_U01;
   gen->GetBits = My16807_Bits;

   strcpy (name, "My LCG implementation for a = 16807:");
   addstr_Int (name, "   s = ", s);
   leng = strlen (name);
   gen->name = util_Calloc (leng + 1, sizeof (char));
   strncpy (gen->name, name, leng);
   return gen;
}

void DeleteMy16807 (unif01_Gen * gen)
{
   gen->state = util_Free (gen->state);
   gen->name = util_Free (gen->name);
   util_Free (gen);
}