1/* ------------------------------------------------------------- */ 2/* File: example_rngstreams.c */ 3/* ------------------------------------------------------------- */ 4#ifdef UNURAN_SUPPORTS_RNGSTREAM 5/* ------------------------------------------------------------- */ 6/* This example makes use of the RNGSTREAM library for */ 7/* for generating uniform random numbers. */ 8/* (see http://statmath.wu.ac.at/software/RngStreams/) */ 9/* To compile this example you must have set */ 10/* ./configure --with-urng-rngstream */ 11/* (Of course the executable has to be linked against the */ 12/* RNGSTREAM library.) */ 13/* ------------------------------------------------------------- */ 14 15/* Include UNURAN header files. */ 16#include <unuran.h> 17#include <unuran_urng_rngstreams.h> 18 19/* ------------------------------------------------------------- */ 20 21int main(void) 22@{ 23 int i; /* loop variable */ 24 double x; /* will hold the random number */ 25 double fparams[2]; /* array for parameters for distribution */ 26 27 /* Declare the three UNURAN objects. */ 28 UNUR_DISTR *distr; /* distribution object */ 29 UNUR_PAR *par; /* parameter object */ 30 UNUR_GEN *gen; /* generator object */ 31 32 /* Declare objects for uniform random number generators. */ 33 UNUR_URNG *urng1, *urng2; /* uniform generator objects */ 34 35 /* The RNGSTREAMS library sets a package seed. */ 36 unsigned long seed[] = @{111u, 222u, 333u, 444u, 555u, 666u@}; 37 RngStream_SetPackageSeed(seed); 38 39 /* RngStreams only: */ 40 /* Make a object for uniform random number generator. */ 41 /* For details see */ 42 /* http://statmath.wu.ac.at/software/RngStreams/ */ 43 urng1 = unur_urng_rngstream_new("urng-1"); 44 if (urng1 == NULL) exit (EXIT_FAILURE); 45 46 /* Use a predefined standard distribution: */ 47 /* Beta with parameters 2 and 3. */ 48 fparams[0] = 2.; 49 fparams[1] = 3.; 50 distr = unur_distr_beta( fparams, 2 ); 51 52 /* Choose a method: TDR. */ 53 par = unur_tdr_new(distr); 54 55 /* Set uniform generator in parameter object */ 56 unur_set_urng( par, urng1 ); 57 58 /* Create the generator object. */ 59 gen = unur_init(par); 60 61 /* Notice that this call has also destroyed the parameter */ 62 /* object `par' as a side effect. */ 63 64 /* It is important to check if the creation of the generator */ 65 /* object was successful. Otherwise `gen' is the NULL pointer */ 66 /* and would cause a segmentation fault if used for sampling. */ 67 if (gen == NULL) @{ 68 fprintf(stderr, "ERROR: cannot create generator object\n"); 69 exit (EXIT_FAILURE); 70 @} 71 72 /* It is possible to reuse the distribution object to create */ 73 /* another generator object. If you do not need it any more, */ 74 /* it should be destroyed to free memory. */ 75 unur_distr_free(distr); 76 77 /* Now you can use the generator object `gen' to sample from */ 78 /* the distribution. Eg.: */ 79 for (i=0; i<10; i++) @{ 80 x = unur_sample_cont(gen); 81 printf("%f\n",x); 82 @} 83 84 /* Now we want to switch to a different (independent) stream */ 85 /* of uniform random numbers. */ 86 urng2 = unur_urng_rngstream_new("urng-2"); 87 if (urng2 == NULL) exit (EXIT_FAILURE); 88 unur_chg_urng( gen, urng2 ); 89 90 /* ... and sample again. */ 91 for (i=0; i<10; i++) @{ 92 x = unur_sample_cont(gen); 93 printf("%f\n",x); 94 @} 95 96 /* When you do not need the generator object any more, you */ 97 /* can destroy it. */ 98 unur_free(gen); 99 100 /* We also should destroy the uniform random number generators.*/ 101 unur_urng_free(urng1); 102 unur_urng_free(urng2); 103 104 exit (EXIT_SUCCESS); 105@} /* end of main() */ 106 107/* ------------------------------------------------------------- */ 108#else 109#include <stdio.h> 110#include <stdlib.h> 111int main(void) @{ 112 printf("You must enable the RNGSTREAM library to run this example!\n\n"); 113 exit (77); /* exit code for automake check routines */ 114@} 115#endif 116/* ------------------------------------------------------------- */ 117 118