1

I am trying to generate random numbers with exponential distribution. I have found Math.NET NuGet package. It looks nice, but I can not figure out how to generate a vector of these kind of data.

I have included the reference and tried something like this (inspired by official website) - I know that Uniform will not generate Exponential values.:

 Generate.Uniform(100);

However I get: "Generate is does not exist in current context. "

I have also tried:

 Random rnd= new Random();
 double[] samples;
 double lambda = 0.1;
 Exponential.Samples(rnd, samples,lambda); 

Here I get "Invalid expression term ')' " and " ; expected" on the last line.

user2179427
  • 331
  • 5
  • 16

1 Answers1

0

The Samples function expects the array to be allocated already, so you can reuse it repeatedly. It will fill the full array.

Random rnd = new Random();
double lambda = 0.1;

double[] samples = new double[200];
Exponential.Samples(rnd, samples, lambda);

However, you've asked for a vector, not an array:

Vector<double> v = CreateVector.Random<double>(200, new Exponential(lambda, rnd));

The error you got is a C# syntax error unrelated to Math.NET Numerics (and not present in the code you actually posted here). For Generate you need to open the MathNet.Numerics namespace.

Christoph Rüegg
  • 4,626
  • 1
  • 20
  • 34
  • Thank you :). One more question. How to set range of generated numbers. Foe rxample between 0 and 1? – user2179427 May 18 '16 at 08:46
  • With uniform distribution: `ContinuousUniform.Samples(rnd, samples, 0.0, 1.0);` Or do you mean with an exponential distribution but filtered to 0.0-1.0? – Christoph Rüegg May 18 '16 at 08:48
  • Yes, I mean with an exponential distribution from 0.0 - 1.0. – user2179427 May 18 '16 at 09:22
  • Assuming a given lambda that would normally produce numbers outside of the range, you could do something along the lines of `Exponential.Samples(rnd, lambda).Where(x => x <= 1.0).Take(200).ToArray()`. Of course, technically such filtered numbers are then no longer strictly exponentially distributed. – Christoph Rüegg May 18 '16 at 19:45