Random number generators discussion

Random number generators | www.agner.org

 
thread GPL & Multinomials - John Bauer - 2009-07-11
last replythread GPL & Multinomials - Agner - 2009-07-12
last replythread GPL & Multinomials - John Bauer - 2009-07-13
last reply Arbitrary discrete distribution - Agner - 2009-07-14
 
GPL & Multinomials
Author:  Date: 2009-07-11 20:56
Hi Agner,

Have you given any thought to releasing your random number libraries under a license other than GPL? I'm working on at least one project that uses a different license, would not want to switch, and would not want to include GPL libraries because of possible license "infection" issues. (This may be a misunderstanding on my part, but I think if a project links to a GPL library, that means it also needs to be GPL.)

Also, what about a multinomial function call that returns a single value, rather than an array of counts? I wrote one myself for convenience, although it is not optimized in any way. Would you be interested in such a method, or would you be willing to write one yourself?

Thanks,

John

   
GPL & Multinomials
Author: Agner Date: 2009-07-12 07:52
John Bauer wrote:
Have you given any thought to releasing your random number libraries under a license other than GPL?
My reason for using GPL license is that non-commercial projects can use it for free, while commercial projects have to pay for a license.

It's a general problem for the open source movement that there are a lot of different open source licenses around, which are not always mutually compatible. I don't know what to do about it. You may want to use Matsumoto's original C code for the Mersenne twister. It has a BSD license.

John Bauer wrote:

Also, what about a multinomial function call that returns a single value, rather than an array of counts?
I don't understand what you mean. A multinomial variate must by definition be a vector.
   
GPL & Multinomials
Author:  Date: 2009-07-13 22:26
> You may want to use Matsumoto's original C code for the Mersenne twister. It has a BSD license.

Cool, thanks for the link. I appreciate the tip, although I do like the many distributions you have already built.

>> Also, what about a multinomial function call that returns a single value, rather than an array of counts?

> I don't understand what you mean. A multinomial variate must by definition be a vector.

I meant: if you only request one sample from the multinomial, and then instead of a vector, you get back the index that would have been set to 1.

   
Arbitrary discrete distribution
Author: Agner Date: 2009-07-14 07:20
John Bauer wrote:
I meant: if you only request one sample from the multinomial, and then instead of a vector, you get back the index that would have been set to 1.
That would in fact be equivalent to defining an arbitrary discrete distribution from a table of the probabilities of each outcome. You can do it in this way:

class CMyDistribution : public CRandomMersenne { // use any random generator as base class
  private:
  double const * prob; double sum; int n;
  public:
  void DefineProbabilities(double const * source, int colors) {
    prob = source; n = colors; sum = 0;
    for (int i=0; i < n; i++) sum += prob[i];}

  int MakeDraw() {
    double R = sum * Random() - prob[0];
    int x = 0;
    while (R > 0. && x < n-1)  R -= prob[++x];
    return x;}
};

int main() {
  int seed = 123;
  CMyDistribution rangen;
  rangen.RandomInit(seed);

  const int ncolors = 4;
  static const double ProbabilityList[ncolors] = {3., 1., 4., 2.};
  rangen.DefineProbabilities(ProbabilityList, ncolors);
  int x = rangen.MakeDraw();

  // This will give a value in the range 0-3 with probabilities 0.3, 0.1, 0.4, 0.2 respectively
  printf("\nrandom draw = %i", x);

  return 0;
}