-
Notifications
You must be signed in to change notification settings - Fork 1
/
SBM.cpp
50 lines (40 loc) · 1.13 KB
/
SBM.cpp
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
#include <Rcpp.h>
#include <vector>
#include <cmath>
#include <random>
using namespace Rcpp;
using namespace std;
// This is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp
// function (or via the Source button on the editor toolbar). Learn
// more about Rcpp at:
//
// http://www.rcpp.org/
// http://adv-r.had.co.nz/Rcpp.html
// http://gallery.rcpp.org/
//
// Define matrix type
typedef std::vector< std::vector<double> > matrix;
// [[Rcpp::export]]
std::vector<double> rnormCpp(int n = 1000, double mu = 0, double sigma = 1)
{
// Création d'un générateur de nombres aléatoires suivant la loi normale
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<double> dist(mu, sigma);
// Génération des nombres aléatoires et stockage dans un vecteur
std::vector<double> values(n);
for (int i = 0; i < n; i++) {
values[i] = dist(gen);
}
return values;
}
// [[Rcpp::export]]
vector<double> cumsumCpp(vector<double> v){
double cum = 0;
for(int i = 0; i < v.size(); i++){
cum += v[i];
v[i] = cum;
}
return v;
}