| 12
 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
 
 | #include <cmath>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <math.h>
#include <string>
#include <ctime>
 
// monte carlo pour une variable aléatoire exponenetielle de paramètre lambda
 
using namespace std;
 
int main(int argc, char *argv[])
{
float x,U,s,v;
int N=100000;  
float lambda=2;
 
srand(time(NULL));                         
s=0; v=0;    // initialisation de la somme de la variable aléatoire (somme de x et x^2)
            for(int i=0;i<N;i++)
 
                {
                    U=(float)rand()/((float)RAND_MAX+1.0);
 
                     x= (-1/lambda)*log(U);
 
                     s=s+x; 
                     v=v+x*x;
                     cout<<"s="<<s<<endl;
                     cout<<"v="<<v<<endl;
 
 
                 } 
 
 
           system("pause");
return EXIT_SUCCESS;
} | 
Partager