File: example_gsl.c

package info (click to toggle)
unuran 1.11.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,344 kB
  • sloc: ansic: 204,607; sh: 4,783; perl: 3,160; makefile: 839; cpp: 24
file content (71 lines) | stat: -rw-r--r-- 2,721 bytes parent folder | download | duplicates (2)
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
/* ------------------------------------------------------------- */
/* File: example_gsl.c                                           */
/* ------------------------------------------------------------- */
#ifdef UNURAN_SUPPORTS_GSL
/* ------------------------------------------------------------- */
/* This example makes use of the GSL library for generating      */
/* uniform random numbers.                                       */
/* (see http://www.gnu.org/software/gsl/)                        */
/* To compile this example you must have set                     */
/*   ./configure --with-urng-gsl                                 */
/* (Of course the executable has to be linked against the        */
/* GSL library.)                                                 */
/* ------------------------------------------------------------- */

/* Include UNURAN header files.                                  */
#include <unuran.h>
#include <unuran_urng_gsl.h>

/* ------------------------------------------------------------- */

int main(void)
{
  int    i;          /* loop variable                            */
  double x;          /* will hold the random number              */

  /* Declare the three UNURAN objects.                           */
  UNUR_DISTR *distr;    /* distribution object                   */
  UNUR_PAR   *par;      /* parameter object                      */
  UNUR_GEN   *gen;      /* generator object                      */

  /* Declare objects for uniform random number generators.       */
  UNUR_URNG  *urng;     /* uniform generator objects             */

  /* GNU Scientific Library only:                                */
  /* Make a object for uniform random number generator.          */
  urng = unur_urng_gsl_new(gsl_rng_mt19937);
  if (urng == NULL) exit (EXIT_FAILURE);

  /* Create a generator object using this URNG */
  distr = unur_distr_normal( NULL, 0 );
  par = unur_tdr_new(distr);
  unur_set_urng( par, urng );
  gen = unur_init(par);
  if (gen == NULL)  exit (EXIT_FAILURE);
  unur_distr_free(distr);

  /* Now you can use the generator object `gen' to sample from   */
  /* the distribution. Eg.:                                      */
  for (i=0; i<10; i++) {
    x = unur_sample_cont(gen);
    printf("%f\n",x);
  }

  /* Destroy objects */
  unur_free(gen);
  unur_urng_free(urng);

  exit (EXIT_SUCCESS);
} /* end of main() */

/* ------------------------------------------------------------- */
#else
#include <stdio.h>
#include <stdlib.h>
int main(void) {
  printf("You must enable the GSL to run this example!\n\n");
  exit (77);    /* exit code for automake check routines */
}
#endif
/* ------------------------------------------------------------- */