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_discr.c */
/* ------------------------------------------------------------- */
/* Include UNURAN header file. */
#include <unuran.h>
/* ------------------------------------------------------------- */
/* Example how to sample from a discrete univariate distribution.*/
/* ------------------------------------------------------------- */
int main(void)
{
int i;
double param = 0.3;
double probvec[10] = {1.0, 2.0, 3.0, 4.0, 5.0,\
6.0, 7.0, 8.0, 4.0, 3.0};
/* Declare the three UNURAN objects. */
UNUR_DISTR *distr1, *distr2; /* distribution objects */
UNUR_PAR *par1, *par2; /* parameter objects */
UNUR_GEN *gen1, *gen2; /* generator objects */
/* First distribution: defined by PMF. */
distr1 = unur_distr_geometric(¶m, 1);
unur_distr_discr_set_mode(distr1, 0);
/* Choose a method: DARI. */
par1 = unur_dari_new(distr1);
gen1 = unur_init(par1);
/* It is important to check if the creation of the generator */
/* object was successful. Otherwise `gen' is the NULL pointer */
/* and would cause a segmentation fault if used for sampling. */
if (gen1 == NULL) {
fprintf(stderr, "ERROR: cannot create generator object\n");
exit (EXIT_FAILURE);
}
/* Second distribution: defined by (finite) PV. */
distr2 = unur_distr_discr_new();
unur_distr_discr_set_pv(distr2, probvec, 10);
/* Choose a method: DGT. */
par2 = unur_dgt_new(distr2);
gen2 = unur_init(par2);
if (gen2 == NULL) {
fprintf(stderr, "ERROR: cannot create generator object\n");
exit (EXIT_FAILURE);
}
/* print some random integers */
for (i=0; i<10; i++){
printf("number %d: %d\n", i*2, unur_sample_discr(gen1) );
printf("number %d: %d\n", i*2+1, unur_sample_discr(gen2) );
}
/* Destroy all objects. */
unur_distr_free(distr1);
unur_distr_free(distr2);
unur_free(gen1);
unur_free(gen2);
exit (EXIT_SUCCESS);
} /* end of main() */
/* ------------------------------------------------------------- */
|