Add your own alternative version
Stats
271.7K views 13.5K downloads 230 bookmarked
Posted
8 Aug 2006

Comments and Discussions



Hello I don't study mathematics and i needs a source code of the program chi square, t student, and others helpme please...
I speak spanish my english is very bad.... lo siento
helpme please





hendryck wrote: i needs a source code of the program chi square, t student, and others
Download the source code of the library using the link at the top of the article and take a look at the respective classes.
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook





Your work was so great and comprehensive and it helped me a lot in doing my project!
Thank you a lot!
Ali Nasri Nazif





Hi,
I see there is a function NextDouble() in the Distribution class and it returns a value between the possible Maximum and Minimum value of the distribution. Is there a way to restrict the numbers produced by the distribution into a smaller interval. Like if you are calling NextDouble() until you get a number in the interval, let's say, [a, b] < [Maximum, Minimum].
Thanks in advance,
Boian





Sogartar wrote: Is there a way to restrict the numbers produced by the distribution into a smaller interval.
The distributions included in the library provide no general way to restrict the interval of generated values. The numbers produced by a distribution are influenced by their individual parameters, so you can restrict the interval if a concrete distribution provides parameters for the minimum and/or maximum.
Regards Stefan
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook





First of all: Great work !
Do you plan do add additional methods to the distribution classes like Quantiles, calculation of probabilities of intervalls etc. ?





THX for the praise
Regarding your question: I'm sorry, but currently I have no plans to add functionality to the library, since I do not have much time.
Regards, Stefan
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de





First of all I'd like to thank you for producing such a comprehensive library.
My code looked something like this to generate Discrete uniformly distributed random numbers between 0.0 and 1.0.
DiscreteUniformDistribution d = new DiscreteUniformDistribution();
And whether i left the Alpha and Beta for the default values or set it to 0 or 1.
i used the NextDouble() method to get a double, but always the generator just generate 0 or 1 values, and no in between numbers.
Thanks alot





All discrete distributions generate only whole numbers regardless of which Next method you call. In your case you should use the ContinuousUniformDistribution .
Regards Stefan
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de






I was triing to test Erlang distribution with parameters aplha=2 and lambda=0.4
Theoretically, I should get the average near 2 / 0.4 = 5.0
After 10000 runs my average was 0.8842
The error is in method NextDouble() of class ErlangDistribution
The following statement
return this.helper1 * Math.Log(1.0  product);
should be rewritten like this
return this.helper1 * Math.Log(product);
Overall I'm very impressed by the library!
Thank you for such great effort!
Herman H.





Hi and thanks for finding this error. The intention of the substract operation was to avoid Math.Log(0.0), but instead of the product the single random numbers have to be substracted from 1.0. I changed the NextDouble method as follows:double product = 1.0;
for (int i = 0; i < this.alpha; i++)
{
product *= (1.0  this.Generator.NextDouble());
}
return this.helper1 * Math.Log(product); The change will be included in the next version of the library. I'll try to publish it as soon as possible but since I'm currently reviewing the whole library and write some UnitTest this may take a while.
Once again thx for the input.
Regards Stefan
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de





MT19937Generator rGen = new MT19937Generator();
Debug.WriteLine("Rgen #: " + rGen.Next()) ;
I ran this code 800 times. New instance of the generator every time. I got the same number on tries 1  799. Am I running it wrong?





The default constructor of the MT19937Generator class (as well as all other generator classes) uses a timedependent seed (Environment.TickCount ). On a fast computer it is very likely that subsequently created instances have the same seed. To see the effect run the following code with and without the Thread.Sleep .
for (int i = 0; i < 10; i++)
{
MT19937Generator gen = new MT19937Generator();
System.Diagnostics.Trace.WriteLine(i + ": " + gen.Next());
System.Threading.Thread.Sleep(1);
}
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de





This is just excellent. Thank you for posting this.





Can someone provide straightforward directions on how to use this Random class in a VC# 2005 project. I've added the project to my solution and set the dependency. But, I can't use the Random Class ... what am I missing?





After adding the project to your solution, you have to add a reference to it in the project that should use the library and probably add a using directive for the Troschuetz.Random namespace at the top of code files that should use classes from the library.
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de





Thanks ... just what I needed. Thanks for the great library!!





First of all I'd like to thank you for producing such a comprehensive library.
However, having just spent an hour or so tracking down a bug in my own code, caused by the behaviour of your library, I'd like to suggest a couple of improvements.
My code looked something like this to generate uniformly distributed random numbers between 4.0 and 8.0.
<br />
ContinuousUniformDistribution u = new DiscreteUniformDistribution(generator);<br />
u.Alpha = 4.0;<br />
u.Beta = 8.0;<br />
Unfortunately, since Alpha and Beta are implicityly initialized to zero, and the Alpha and Beta property setters check that Alpha < Beta, the u.Alpha = 4.0; silently fails because 4.0 is not less than the current value of Beta == 0.0 at that point. This results in Alpha remaining at zero, although Beta will be set successfully.
You suggest calling IsValidAlpha and IsValidBeta to check the values, but these are called internally by the property setters anyway, and will still fail for the reasons outlined above.
Obviously, the workaroud is
<br />
ContinuousUniformDistribution u = new DiscreteUniformDistribution(generator);<br />
u.Beta = 8.0;<br />
u.Alpha = 4.0;<br />
but this is very nonobvious from the API.
May I suggest removing the checks in the Alpha and Beta property setters, and introducing a new AreValidAlphaBeta() so we can do something like:
<br />
ContinuousUniformDistribution u = new DiscreteUniformDistribution(generator);<br />
u.Alpha = 4.0;<br />
u.Beta = 8.0;<br />
Debug.Assert(u.AreValidAlphaBeta());<br />
or even
<br />
ContinuousUniformDistribution u = new DiscreteUniformDistribution(generator);<br />
Debug.Assert(u.AreValidAlphaBeta(4.0, 8.0));<br />
u.Alpha = 4.0;<br />
u.Beta = 8.0;<br />
Other that this wart, its an excellent library. Thank you again.





Hi!
First of all, I apologize for the inconvenience caused. I'm truly sorry that this "silent failure" of my library made you waste an hour.
To be honest, I never was fully content with the design of the properties for the distribution parameters and the possibility of "silent failure" it introduces. I chose this design, cause I did not like the idea of throwing exceptions from the property setters but at the same time wanted to prevent invalid configuration of a distribution. I promise you that I will review the design considering your suggestion for which I am grateful. However, I am currently really busy with the completion of my master's thesis, so this must wait till the next month.
Regards, Stefan
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de





Stefan,
No need to apologise. I appreciate having your library  it has saved me far more time than it has cost.
Best of luck with completing your thesis!
Rob





Actually, the workaround I gave above is not robust in all circumstances. For uniform distributions the following code will work, irrespective of the original and new values of Alpha and Beta.
<br />
// A routine to work around the misfeature in setting alpha and beta<br />
private static void AlphaAndBeta(DiscreteUniformDistribution distribution, int alpha, int beta)<br />
{<br />
<br />
distribution.Alpha = Math.Min(alpha, distribution.Alpha);<br />
distribution.Beta = Math.Max(beta, distribution.Beta);<br />
<br />
distribution.Alpha = alpha;<br />
distribution.Beta = beta;<br />
Debug.Assert(distribution.Alpha == alpha);<br />
Debug.Assert(distribution.Beta == beta);<br />
}<br />





I'm having no problems resetting a Standard Generator, but resetting a distribution doesn't seem to be working for me:
Troschuetz.Random.NormalDistribution n = new Troschuetz.Random.NormalDistribution ();<br />
string s1="First Try of 5 Random Numbers: ";<br />
for (int i=1; i<=5; i++)<br />
{<br />
s1+=n.NextDouble() + " ";<br />
}<br />
<br />
n.Reset();<br />
<br />
string s2= "Second Try of 5 Random Numbers:";<br />
for (int i=1; i<=5; i++)<br />
{<br />
s2+=n.NextDouble () + " ";<br />
}<br />
MessageBox.Show (s1 + "\r\n" + s2);<br />
The above code ought to produce the exact same set of 5 numbers, and it doesn't. What am I doing wrong?
PS. Thank you very much for thisit looks well put together! Sorry if I'm missing something obvious...





Hi,
thanks for the input and finding this error. The problem is that I've once more forgotten to consider the random number generation algorithm of the normal distribution. It computes two random numbers at a time, buffers one and returns it when the NextDouble method is called for the next time. When I started developing this library I thought it will be sufficient for all distributions to reset the underlying random number generator, but obviously it is not since for example the NormalDistribution has to discard an already computed random number. I've corrected the whole mess by declaring the Distribution.Reset virtual and overriding it in derived classes when necessary. The new version of the library is uploaded.
Regards, Stefan
"Programming today is a race between software engineers striving to build bigger and better idiotproof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."  Rick Cook www.troschuetz.de





This is awesome, thank you a million!







General News Suggestion Question Bug Answer Joke Praise Rant Admin Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

