Department of Computer Science | Institute of Theoretical Computer Science | CADMO

Theory of Combinatorial Algorithms

Prof. Emo Welzl and Prof. Bernd Gärtner

// Prog: loaded_dice.cpp // implement a class for rolling a loaded dice. #include namespace ifm { // class loaded_dice: implementation loaded_dice::loaded_dice (double p1, double p2, double p3, double p4, double p5, ifm::random& generator) : p_upto_1 (p1), p_upto_2 (p_upto_1 + p2), p_upto_3 (p_upto_2 + p3), p_upto_4 (p_upto_3 + p4), p_upto_5 (p_upto_4 + p5), g (generator) {} unsigned int loaded_dice::operator()() { const double x = g(); if (x <= p_upto_1) return 1; if (x <= p_upto_2) return 2; if (x <= p_upto_3) return 3; if (x <= p_upto_4) return 4; if (x <= p_upto_5) return 5; return 6; } } // end namespace ifm