3

Ok, it is possible to give weights/probabilities in boost::random::discrete_distribution.

e.g.

double probabilities[] = { 0.5, 0.1, 0.1, 0.1, 0.1, 0.1 };

boost::random::discrete_distribution<> dist(probabilities);

Question: Once the object dist is constructed

(1)How to change one of the weights e.g. 0.5 to 0.3?

(2) How to reassign all the weights at once?

911
  • 908
  • 8
  • 16

1 Answers1

3

Create a new distribution object and use that instead.

spraff
  • 32,570
  • 22
  • 121
  • 229
  • I thought of creating the object and then alter the weights in a loop. So was worried about the overhead of creating new object every time. – 911 Jan 19 '12 at 11:56
  • 1
    @sam: This is premature optimization. Have you measured any performance impact by recreating a new object ? You should write your code so it works well and is easy to maintain, then, **and only then**, if you see performance issues, worry about these concerns. – ereOn Jan 19 '12 at 11:58
  • 1
    Don't worry about the overhead of object creation, it's running an iteration of the random engine that is the problem. **Measure first, optimise later.** – spraff Jan 19 '12 at 11:59
  • @ereOn: to make the overhead minimum and make faster. – 911 Jan 19 '12 at 11:59
  • @spraff, profiling shows that object creation is by far the bottleneck if generating only one random number for each set of weights. In this case, it seems that the best solution is coding the discrete generator ourselves (based on c++'s uniform 0-to-1 generator). – rd11 Aug 12 '15 at 13:29
  • Note: this led to 10x speedup in my case. – rd11 Aug 12 '15 at 14:16