random numbers in boost 1.5.2

4
Boost Random Number Examples Mauricio Bedoya [email protected] February 2014 Examples avoid any warning message, but does not assert any incorrect distribution parametriza- tion. Enjoy and welcome to boost. 1. Normal Distribution Random Numbers 1 #include <iostream> #include <boost/random/mersenne twister.hpp> 3 #include <boost/random.hpp> #include <boost/random/variate generator.hpp> 5 #include <boost/lexical c a s t . hpp> 7 typedef double Real ; using namespace std ; 9 int main() 11 { typedef boost : : mt19937 ENGINE ; 13 typedef boost :: normal distribution <double > DISTRIBUTION ; typedef boost :: variate generator < ENGINE , DISTRIBUTION > GENERATOR; 15 ENGINE engine(boost :: numeric cast <int >(time(0))); 17 const Real mean = 0; const Real sigma = 1; 19 DISTRIBUTION pdf( mean , sigma ) ; GENERATOR generator(eng,pdf); 21 for (int i = 0; i < 10; i ++) 23 std : : cout << generator () << std ::endl; 25 return 0; } Alternative (only change line 17); 1

Upload: mauricio-bedoya

Post on 19-Oct-2015

39 views

Category:

Documents


2 download

DESCRIPTION

Examples of how to generate random numbers in boost. Implemented in Xcode 4.5.2

TRANSCRIPT

  • Boost Random Number Examples

    Mauricio Bedoya

    [email protected] 2014

    Examples avoid any warning message, but does not assert any incorrect distribution parametriza-tion. Enjoy and welcome to boost.

    1. Normal Distribution Random Numbers

    1 #inc lude #inc lude

    3 #inc lude #inc lude

    5 #inc lude

    7 typede f double Real ;us ing namespace std ;

    9i n t main ( )

    11 {typede f boost : : mt19937 ENGINE;

    13 typede f boost : : no rma l d i s t r i bu t i on DISTRIBUTION;typede f boost : : v a r i a t e g ene ra t o r GENERATOR;

    15ENGINE engine ( boost : : numeric cast(time (0) ) ) ;

    17 const Real mean = 0 ;const Real sigma = 1 ;

    19 DISTRIBUTION pdf (mean , sigma ) ;GENERATOR generator ( eng , pdf ) ;

    21f o r ( i n t i = 0 ; i < 10 ; i++)

    23 std : : cout

  • #inc lude 2 #inc lude #inc lude

    4 #inc lude #inc lude

    6typede f double Real ;

    8 us ing namespace std ;

    10 i n t main ( ){

    12 typede f boost : : mt19937 ENGINE;typede f boost : : no rma l d i s t r i bu t i on DISTRIBUTION;

    14 typede f boost : : v a r i a t e g ene ra t o r GENERATOR;

    16 ENGINE engine ( boost : : numer ic cast( c l o ck ( ) ) ) ;const Real mean = 0 ;

    18 const Real sigma = 1 ;DISTRIBUTION pdf (mean , sigma ) ;

    20 GENERATOR generator ( eng , pdf ) ;

    22 f o r ( i n t i = 0 ; i < 10 ; i++)std : : cout

  • Now I will expose the template function for other 3 distributions. Only change the distributionswith the corresponding parameters. For more information visit: Boost Distributions

    3. Template Function Uniform Random Numbers

    Similar to Matlab function (only returns a vector). Assume that a

  • 4. Template Function Poisson Random Numbers

    Similar to Matlab function (only returns a vector).1 #inc lude #inc lude

    3 #inc lude #inc lude

    5 #inc lude

    7 typede f double Real ;us ing namespace std ;

    9template

    11 vector po i s s rnd ( const i n t& n , const Real& lambda ){

    13 typede f boost : : mt19937 ENGINE;typede f boost : : p o i s s on d i s t r i bu t i o n DISTRIBUTION;

    15 typede f boost : : v a r i a t e g ene ra t o r GENERATOR;

    17 ENGINE engine ( boost : : numeric cast( c l o ck ( ) ) ) ;DISTRIBUTION pdf ( lambda ) ;

    19 GENERATOR generate ( engine , pdf ) ;

    21 vector random numbers ;f o r ( i n t i =0; i !=n ; ++i )

    23 {random numbers . push back ( generate ( ) ) ;}

    25 return random numbers ;}

    27in t main ( )

    29 {Real lambda = 1 ;

    31 vector va lues = poissrnd(100 , lambda ) ;}

    5. Template Function Chi2 Random Numbers

    Similar to Matlab function (only returns a vector).#inc lude

    2 #inc lude #inc lude

    4 #inc lude #inc lude

    6typede f double Real ;

    8 us ing namespace std ;

    10 templatevector chi2rnd ( const i n t& n , const Real& V)

    12 {typede f boost : : mt19937 ENGINE;

    14 typede f boost : : random : : c h i s qua r ed d i s t r i bu t i o n DISTRIBUTION;typede f boost : : v a r i a t e g ene ra t o r GENERATOR;

    16ENGINE engine ( boost : : numeric cast( c l o ck ( ) ) ) ;

    18 DISTRIBUTION pdf (V) ;GENERATOR generate ( engine , pdf ) ;

    20vector random numbers ;

    22 f o r ( i n t i =0; i !=n ; ++i ){random numbers . push back ( generate ( ) ) ;}

    24return random numbers ;

    26 }

    28 in t main ( ){

    30 Real V = 6 ;vector va lues = chi2rnd(100 ,V) ;

    32 }

    4

    Normal Distribution Random NumbersTemplate Function Normal Random NumbersTemplate Function Uniform Random NumbersTemplate Function Poisson Random NumbersTemplate Function Chi2 Random Numbers