I am writing a discrete distribution random number generator in a c++ class. The requirements are:
- I don't want to create a discrete_distribution object each time I use it. I know distribution object is lightweight, but my weight array is so long that the overload is still too high. Also, I need to use the distribution object in different functions in this class.
- I need to change the distribution (weight array) from time to time
- I don't know the exact distribution (weight array) when the class is constructed
Currently I have two solutions, after initialize random number engine with random_device rd; mt19937 engine;
in class and engine(rd())
in initialize list.
One is to create the discrete_distribution object with discrete_distribution<> *d=new discrete_distribution<>(weight_array,weight_array+weight_array_size)
and store the pointer in the class. Each time I call (*d)(engine) to generate a random number, and I just need to delete the distribution and make a new one to update the weight array.
Another way is to define discrete_distribution<> d
in class and update weight array with d=discrete_distribution<>(weight_array,weight_array+weight_array_size)
, so that we can generate random number with d(engine)
and don't need to worry about pointers.
But it seems that both ways are not classical way to use c++ objects. Are they wrong? Are there any drawbacks to write code this way?
Thanks
Aucun commentaire:
Enregistrer un commentaire