aGrUM  0.16.0
utils_random_tpl.h
Go to the documentation of this file.
1 
30 namespace gum {
31 
32  // return a random discrete distribution
33  // @param n is the number of modalities for the ditribution
34  template < typename GUM_SCALAR >
35  std::vector< GUM_SCALAR > randomDistribution(Size n) {
36  if (n < 2) n = 2;
37 
38  std::vector< GUM_SCALAR > v(n);
39  GUM_SCALAR s;
40 
41  do {
42  for (Idx i = 0; i < n; i++) {
43  v[i] = (GUM_SCALAR)randomProba();
44  }
45 
46  s = std::accumulate(v.begin(), v.end(), (GUM_SCALAR)0.0);
47 
48  } while (s < (GUM_SCALAR)(1e-5));
49 
50  for (Idx i = 0; i < n; i++) {
51  v[i] /= s;
52  }
53 
54  return v;
55  }
56 } // namespace gum
std::vector< GUM_SCALAR > randomDistribution(Size n)
Return a random discrete distribution.
double randomProba()
Returns a random double between 0 and 1 included (i.e.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Size Idx
Type for indexes.
Definition: types.h:53
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48