-1

Is there any cpp's equivalent of the randsample in Matlab. The function randsample in Matlab is as follows:

y = randsample(n,k,true,w)

which returns a weighted sample taken with replacement, using a vector of positive weights w, whose length is n. The probability that the integer i is selected for an entry of y is w(i)/sum(w).

for example:

R = randsample('ACGT', 10,true,[0.15 0.35 0.35 0.15])

will have an output

CTTCGTCGGG

If there is no existing cpp lib having the same function as randsample in Matlab, how to write the equivalent function in cpp?

Kelvin Tan
  • 982
  • 1
  • 12
  • 33

1 Answers1

0

The Random library of boost helps a lot with this kind of stuff. It's not direct but a least they give you a way to draw samples from a discrete distribution defined by weights.

  • Since c++11 there is no need to use boost because you can use the std::discrete_disitribution class. http://en.cppreference.com/w/cpp/numeric/random/discrete_distribution – Gottfried Aug 15 '15 at 10:50