Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,
I've got one more question: do you actually feel any difference between pseudorandom number generator (PRNG) based on using std::rand() library function in C and C++, and similar functions, libraries and packages implemented for C#.NET ?

You know, when I started to use PRNG's in C# such as System.Random() class and also MathNet.Nurmerics, I've noticed that such functional PRNGs for C#.NET provide much worse results of random numbers generation rather than a simple std::rand() function in C and C++.

Still, I simply can't figure out why ?? Smile | :)

Since then, I even implemented my own PRNG based on Box-Muller transformation and such things as _rdtsc() which is Read Time Stamp CPU instruction to achieve a better results in random number generation arena. Smile | :)

Finally, can anyone guide me how to use multiple PRNGs for C#.NET to have similar results as if the std::rand() function is used ??

What I have tried:

Since then, I even implemented my own PRNG based on Box-Muller transformation and such things as _rdtsc() which is Read Time Stamp CPU instruction to achieve a better results in random number generation arena. Smile | :)
Posted
Updated 3-Aug-17 1:37am
Comments
CPallini 3-Aug-17 7:52am    
"provide much worse results"
Could you elaborate, please?
Arthur V. Ratz 3-Aug-17 8:08am    
Now, I'm working on the code in both C++ and C# that implements an AI genetic algorithm. As you've probably might know, the initial population of chromosomes in this case is randomly generated by using PRNGs. Specifically, for the genetic algorithm initialization purposes it's strongly recommended to use normal random distribution created by using for example Box-Muller transformation or suppose MersenneTwister. When I normally use std::rand() function in C++ which is very simple, I obviously get the desired quality of random number generation.

Another case is libraries and packages for C#.NET such as built-in System.Random() or MathNet.Numnerics library. In spite of that those libraries for C# allow to either perform plain random numbers generation or sampling to get various distributions including normal distribution, all those libraries for C# really don't do what they're all supposed to.

To be more specific, I normally get very different results on the code that implements a genetic algorithm in the either C++ or C#, at the most part because of the random number generator used. When I run the code written in C++ based on using std::rand() function everything is going well, and not when running C# code unless otherwise.
CPallini 3-Aug-17 8:30am    
Do you mean that random numbers obtained from uniform distribution, transformed with Box-Muller, are much better than the ones obtained from C#? Did you made a statistical test?
As a side note, modern C++ provide more sopishicated (than rand) random number generations, see
http://www.cplusplus.com/reference/random/
Arthur V. Ratz 3-Aug-17 8:35am    
Thanks for guidance. I really have not done any stats tests yet at now. As for more sophisticated STL C++ PRNGs I know about them, but I would have liked to stress that at least simple std::rand() normally provides better results than those similar functions in C#.NET.
Arthur V. Ratz 3-Aug-17 8:36am    
Anyway, I'd kindly appreciate for any ideas how to make using PRNGs in C# better.

1 solution

This CodeProject article might be of interest to you: Random Number Generator Recommendations for Applications[^]
 
Share this answer
 
Comments
Arthur V. Ratz 3-Aug-17 7:37am    
Yes, thanks a lot. I've noticed this article was published a week ago, but I decided to read it later on and lost it. I know this is a good article and I will read it soon for sure.

Thanks for your reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900