Add your own alternative version
Stats
250.4K views 12.7K downloads 224 bookmarked
Posted
8 Aug 2006

Comments and Discussions



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.
// A routine to work around the misfeature in setting alpha and beta
private static void AlphaAndBeta(DiscreteUniformDistribution distribution, int alpha, int beta)
{
distribution.Alpha = Math.Min(alpha, distribution.Alpha);
distribution.Beta = Math.Max(beta, distribution.Beta);
distribution.Alpha = alpha;
distribution.Beta = beta;
Debug.Assert(distribution.Alpha == alpha);
Debug.Assert(distribution.Beta == beta);
}





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 ();
string s1="First Try of 5 Random Numbers: ";
for (int i=1; i<=5; i++)
{
s1+=n.NextDouble() + " ";
}
n.Reset();
string s2= "Second Try of 5 Random Numbers:";
for (int i=1; i<=5; i++)
{
s2+=n.NextDouble () + " ";
}
MessageBox.Show (s1 + "\r\n" + s2);
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!





Hi, my name is Todd and I am a working on a simulation model in VB.NET. I'm a novice programmer and trying to figure things out as I go, but need some help. I need to be able to choose a random number from a normal distribution with a mean and standard deviation (both need to be user defined). Would this library help me? Any suggestions would be extremely helpful. Thanks.
Take care
T





Hi Todd!
You can customize the normal distribution (and all other distributions too) with help of its (their) parameters exposed as properties. In case of the normal distribution the mean directly maps to the μ parameter and the standard deviation directly maps to the σ parameter. In general, the relation of the parameters to the statistics of a distribution is shown in the respective Wikipedia articles to which the article links.
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





Thanks a lot for the work. Cheers





A wonderful bit of work.
I wanted to use your tester program but it gives a zillion problems with respect to the ZedGraph stuff. For example it says that it can't tell if it's using a ZedGraph or a System.Windows Label. If I disambiguate those then I get a bunch of other serious errors.
As I'm sure it worked on your system, I suspect that the problem arises from the fact that I have a recent ZedGraph installation on my PC and for some reason it's clashing with yours. However, my version of the .dll is identical to yours.
Have you any idea of how I can get your Tester working?
Thanks
Steve





SteveAbbott wrote: As I'm sure it worked on your system
You're right
Nevertheless I checked again and found something strange. Though I copied ZedGraph.dll version 5.0.1.41097 into the RandomTester project and referenced it, Visual Studio tells me in the properties that it references version 4.2.1.35091 at a path that no longer exists and copied it to the output directory (don't know from where). I've experimented a bit, but now everything is messed up and I'm no longer able to let the RandomTester project reference the Random project which is quite essential
I will try to fix this mess on weekend and update the downloads if I succeed.
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





Thanks for trying! I still get confused about what magic DotNet is doing with control dlls. DotNet is often that sad mixture of Microsoft being so smart that it's dumb.
Good luck with your search for the magic fix. I look forward to experimenting with your excellent code.
Steve





Got some time tonight and think I fixed it.
It seems it was some weird problem with the VS project. I copied all resources to a new project where I could reference my Random project again and also the ZedGraph.dll version I want. When referencing the version of ZedGraph which I thought using the whole time, I faced the same ambiguity problems as you and they seem to have changed some other things too. So obviously on your computer the VS project didn't show the weird behaviour as on mine.
Cause I didn't want to rewrite the code for the newer ZedGraph version and the RandomTester runs well with the older one, I stick to it and updated the project so it hopefully uses this one correctly on every computer.
"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





That's brilliant. It all works fine now, thanks for repackaging everything.
I wish I knew a foolproof way of moving a .Net project from one folder to another without all sorts of collateral damage!
Steve





Hi there, first of all *great* article. Very useful for me.
I was wondering if in your work you have came accross an FInv funtion (return the quantile according to the F distribution  avaialble in Excel FInv).
Thanks a lot,
Busi





Sorry, but that method never crossed my way.
"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 Stephan.
If you will, drop me a line...
Thanks,
Busi





I'll keep my eyes open
"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





Hello Stefan (again),
Just asking if during your recent work you have come accross the naughty FInv function...
Thanks,
Busi





Sorry to disappoint you but it still didn't cross my way.
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





Almost ten years ago I took a class in Simulations and Testing for my CE Masters. In that class I had to hand code a half dozen different random number generators in order to to use them simultaneously for different variables/inputs to guarantee they were related. Boy could I have used something like this back then ... though C# wasn't even on the radar then.
Worth a 5 to me even though I don't have a current need to RNG functions.
One thing though, my text book back then had many examples of bad RNG functions and charts/graphs to show why. They were often time series plots in 3D that would show patterns, etc. If I recall correctly, often it wasn't the algortithm, but the parameters that could make or break an RNG. I no longer have the book, but I was wondering how hard it would be to add such tests to your test app so that you could experiement with the parameters to the different RNG functions &/or create more and be able to spot a bad one.





Scott S. wrote: how hard it would be to add such tests to your test app
It's hard to say without having/knowing the book you refer to and therefor not knowing how complex these test are.
"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





Hello
I am trying Random Tester and the DescreteUniformDistribution. And it gives me quite strange results. The histogram is almost OK: the numbers are uniformly distributed, except for 3 points in it. First is the number something less then 1/3 of the range, then the number less then 2/3 of the range and then the upper bound.
If I generate numbers between 0  100, than the histogram is OK except for number(s) a bit less than 30 and 60 and at the number 100.
If I generate numbers between 0  5000, than the histogram is OK except for number(s) about 1400 and 2900 and 5000. To see this anomaly, you have to increase number of samples (I used 10 000 000)
To see it cleary is goot to not use large range (say 0  1000), and use many samples (it's visible in 1 000 000 samples, but much better in 10 000 000 samples).
Can anybody explain this to me?
Thanks a lot, Stano
PS: Great work and thanks a lot for this library.





Hi,
the described behaviour results from the used computation of the displayed histogram. In case you generate discrete random numbers between 0 and 100 there are 101 discrete numbers that occur. At the same time the histogram uses by default only 100 steps (intervals), so it cannot differentiate all generated numbers.
Therefor increase the number of histogram steps, so it equals at least the number of generated discrete numbers.
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





Wonderful article on Codeproject. Thanks heaps.
I was looking for a way to generate randomised stockmarket data for testing puposes. Random generation of close signal is relatively straight forward. The problem is generating open/high/low/close/volume data which is stochastic i.e. some pattern is present related to CLV (Close Location Volume), ATR(average true range) , OBV(On Balance Volume) etc.
At least with your set of random generators I can now simulate stochastics, by applying various distributions to OHLC prices and volumes.
I,m just beginning tonight with your classes and already I have created a better simulator using your classes than I had been able to create previously.
Cheers
Anton





Anton,
How are you accounting for the infamous 'fattails' of the stock market? And do you know of a way to take a distribution based upon historical data and test based on this distribution?
BW





Variation in the distribution used (or parameters or both) whilst travelling between price points gives adequate results.





Thanks for making this available. I am a hydrologist using C# and will find this library very useful in fabricating 'real world' simulations.
I can start weening myself off the Microsoft Random library!





Thank you for this great work. Can't wait to see more update.





Excellent work, thanks!
Do you mind if I merge/integrate your random library with/to the Math.NET Iridium [2] package (it is LGPL, too)? We already have some distributions and generators implemented, but your implementation seems to be superior. Of course we would retain your copyright notice, and mention the contribution on the website, as usual.





Hi Christoph,
I wouldn't mind if you're using my work. In fact I feel quite honored
"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






Its all very nice, but has one very annoying feature.
The 'next' functions all have side effects. It doesn't take much work to rewrite this as side effect free. The end result is that testing is much easier, and you can use assertions easier. For details, have a read of Object Oriented Software Construction, edition 2 by B Meyer.
Nick





Nick Leaton wrote: The 'next' functions all have side effects.
Erm, what do you mean by side effects? What side effect does the Next method exhibit?
Marc
XPressTierSome people believe what the bible says. Literally. At least [with Wikipedia] you have the chance to correct the wiki  Jörgen Sigvardsson People are just notoriously impossible. DavidCrow There's NO excuse for not commenting your code.  John Simmons / outlaw programmer





Having next return both the next random number and move the pointer on makes it difficult for the reasons I have mentioned.
Its better to have a function that returns the current random number, and a subroutine called next that moves the sequence to the next random number.
In otherwords, code up a random number generator just as you would code up an interator
Nick





I think he modeled it after the way Microsoft did it: System.Random[^]
IMO, consistency is better.
Logifusion[^]
If not entertaining, write your Congressman.





All random number generators I've ever worked with from Fortran, C, C++ etc behave in this manner  anything else would be inconsistant. A random number generator is (ideally) an infinite stream of uniform variates so the state should change each time Next() is called.





I'm not sure what Nick Leaton is refering too. If a sideeffect means I have one less call to make then I like that 'side effect'. Also from his commetns I can't figure out what exactly the side effect is. The comment that:
'Having next return both the next random number and move the pointer on makes it difficult for the reasons I have mentioned.'
unfortunately doesn't help because the reasons mentioned are a reference to a book chapter. I would be very interested to know the nature of the obscure side effect. I do a lot of Monte Carlo simulations and genetic algorithm work and most (all) random number generators I've seen work using the sideeffect method, i.e one call to get the next random number.
By the way, great contribution to the .NET community. Thanks very much.





I suspect Nick isn't familiar with the science behind random variates and is trying to apply "OO" principles, i.e. a method call shouldn't both return a value and change the state of the object  The state being the current point in the sequence. This is not a case where that "rule" should be applied
That theory may work when you're traversing over a normal collection  it makes sense to have 2 methods, i.e. MoveNext() and Current. But in this case you almost never want to reuse a random variate and should always modify state to ensure that the caller doesn't forget to advance the sequence. It would be a disaster if you resused the same value twice in a Monte Carlo simulation.
To the original author  good work.





First, I'd like to say that this is a great component, and a lot of work has obviously gone into it. I'd like to know, however, if anyone has noticed a small amount of kurtosis (like I have) in the random normal variates this component generates? If so, can anyone tell how to fix that?





Hi,
I am interested in this topic too. I am looking for a secret "random" generator but after reading this: http://members.cox.net/srice1/random/newerrng.html[^ I think we'd better wait for hadware giant to integrate such as a quantum radom generator in their product. Do you agree?
Where should I go tomorow?
From Vietnam





To satisfy both viewpoints, maybe you could implement a Peek() method. I'd think this would require your generator to go ahead and access the "next" number ahead of any client code's request for the next value  caching it until the client requested it via Next (which would return the cached "next" number, advance the pointer, and get another value ready) or Peek (which would simply return the cached "next" number.
Pat







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.

