Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm currently expanding a simulation model that relies heavily upon random normal distributed numbers. Currently it uses std::normal_distribution, but it's too slow.
Is there a way to implement a fast random normal distribution? The values don't have to be unique as long as it follows a normal distribution.
The Ziggurat generator by Marsaglia and Tsang (JSS, 2000) has been seen to be both statistically sound and fast. You do however want the revised Ziggurat normal generator which uses a better underlying uniform generator as per the comment of Leong et al (JSS, 2005).
I have the original papers as well as a few review papers in this GitHub directory which is part of a Ziggurat C++ implementation for R.
std::normal_distribution is a distribution, not a generator. You may want to try out other generator from the standard library and see what's fastest (see http://en.cppreference.com/w/cpp/numeric/random).
If the quality of the numbers is not that important, you could fall back to very simple algorithms, as simple as taking r=++i%6, for example.
A Linear congruential generator is easy to implement, too (actually, most standard rand() implementation use some implementation thereof. However, it will probably not be faster than using std::linear_congruential_engine. At the above linked site there's also a number of readily "configured" random number generators.
There's also an algorithm by the late Marsaglia, which can be found here and which is quite efficient w.r.t. performance and number quality.
At the end, make sure to use proper optimization flags when testing for performance.
The standard <random> library allows to produce random numbers using combinations of generators and distributions. You are using the normal distribution, and what you want is a faster generator then the default, have a look here.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm writing a genetic algorithm to solve the Master Mind game. I've done lots of research on best approaches and it's incredibly important to have a diverse population. I'm trying to determine how to get really good random numbers in C++. I've done srand(time(NULL)) at the start of my program to set the seed then I've simply used rand(). What I would like to know is how random is that really? Is it pretty good? Are there other better libraries for random numbers?
I know number theory and randomness is a very complicated subject; do you have any pointers in writing your own version of rand()?
For crypto, you need very strong properties on your random numbers. Much of the literature out there focusses on these sorts of requirements. A typical solution would be seeding iterated applications of SHA-256 using environmental noise (hard drive delays, network packets, mouse movements, RDRAND, HAVEGE, ...).
For Monte-Carlo simulations or AI applications, the randomness requirements are much lower indeed. In fact, very simple generators will be good enough. The most basic is the infamous Linear Congruential generator, which is considered a bit old-fashioned now, because the output patterns sometimes produce noticeable and unwanted sampling effects (in particular, some experimental studies done in the 70s and 80s are quite probably flawed because of this). These days, the Mersenne Twister is more popular, and is more than adequate for a computer game. It's available in the C++ standard library: see std::mt19937.
rand()'s randomness is really bad. It is a bog standard LCG and generally has bad randomness and bad quality of projection. If you are serious about quality of randomness for your application, you need to go with something better. Then it depends on whether you want to keep to the standard library, or not.
If you want to use standard library, go with <random> header and Mersenne Twister.
But, I would recommend to you to use the PCG random family instead. Its fast, it has good quality and fixes most of mistakes of <random>.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I have to apply a heuristic algorithm for finding a minimum or maximum from a function.
I understood what heuristic means, but where can I find an algorithm for applying it on Rosenbrock function, for example.
(C++,JAVA,C# or even pseudocode could be very helpful).
The simplest, and most obvious, solution would be to use the Random walk algorithm. It works by starting with a random point within the search space and then visiting it's random neighbor.
A similar, but more reasonable, algorithm is the Hill climbing. Again, you start at a random point but this time you chose the best neighbor.
Another, technically, heuristic algorithm is Random sampling, which just means picking any point from the search space and remembering the best one you found.
An improvement over Random sampling is the Simulated annealing algorithm. It's a kind of a fusion of Random sampling and Hill climbing: you start with picking random points in the search space but as the time goes on you tend to stick with the higher quality ones.
You can find more information and pseudo code samples of all of the above on Wikipedia.
A whole different class of solutions are the Genetic algorithms. You can start learning about them by reading http://www.obitko.com/tutorials/genetic-algorithms/index.php. Unfortunately it does not seem to have any code samples.
The Wikipedia article that you reference mentions adaptive coordinate descent, which is a state-of-the-art evolutionary algorithm, as a technique for minimizing the Rosenbrock function. Googling that finds several papers with pseudocode and algorithms including this one. The paper even includes a reference to actual code in Matlab.
You could also use Expectation–maximization with random restart although that's probably significantly less efficient than adaptive coordinate descent.
You need to use partial derivatives of the function to find its extrema. You start from a random point on the plane and use these derivatives (calculated numerically) to find a direction to move to a next point. Repeat this process until you find a minimum (justified by second derivatives).
By the way, the function you are talking about is a known difficult case for such iterative search, so you'll need to experiment with step size and other parameters of your algorithm.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I am currently prototyping some algorithms in Matlab that rely on matrix, DSP, statistics and image analysis functionality.
Some examples of what I may need:
eigenvectors
convolution in 2D and 3D
FFT
Short Time Fourier Transform
Hilbert transform
Chebyshev polynomials
low pass filter
random multivariate gaussian numbers
kmeans
Later on I will need to implement these algorithms in C++.
I also have a license for Numerical Recipes in C++, which I like because it is well documented and have a wide variety of algorithms.
I also found a class that helps with wrapping NR functions in MEX:nr3matlab.h.
So using this class I should be able to generate wrappers that allow me to call NR functions from Matlab. This is very important to me, so that I can check each step when porting from Matlab to C++.
However Numerical Recipes in C++ have some important shortcomings:
algorithms implemented in a simple, and not necessarily very efficient
manner
not threaded
I am therefore considering using another numerical library.
The ideal library should:
be as broad in scope and functionality as possible
be well documented
(have commercial support)
have already made Matlab wrappers
very robust
very efficient
threaded
(have a GPU implementation that can be turned
on instead of the CPU with a "switch")
Which numerical library (libraries) would you suggest?
Thanks in advance for any answers!
You have a pretty long list of requirements, and it may be challenging to cover them all with a single library.
For general Matlab-to-C++ transitions, I can highly recommend Armadillo which is a templated C++ library with a focus on linear algebra --- and a given focus on making it easy to write Matlab-alike expression. It as very good performance, is very well documented and actively maintained. You could start there and try to fill in the missing pieces for your task.
Actually you should have a look at openCV.
Although its first goal is computer vision/image processing, this library has a lot of linear algebra tools (Almost all that you ask for). At first, this library has been implemented by intel, with a lot of focus on performance. It can handle multi thread, IPP,...
The syntax is rather easier to use than usual C++ library.
You should have a look at this cheat sheet. The syntax has been changed since version 2.0 to mimic matlab.
This library is broadly used, and well active (last big update August 2011).
NAG could be one good option. Loads of financial institutions use it in their mathematical libraries. Don't have a GPU implementation though, when I last used it.
there is also the Eigen library: http://eigen.tuxfamily.org
but it is mostly used as part of a larger framework. It offers basic (and a bit more complex) algebra
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
Which library do you use for N-dimensional arrays?
I use blitz++ at work and I really dislike some aspect of it.
Some aspect of it are even dangerous. The need for resizing before
using operator=. A(Range::all(), Range::all()) throws for an (0,0)
matrix, etc. and the linear algebra operations are to be
done via clapack.
I used and loved eigen. I appreciate its "all-in-header" implementations,
the C++ syntactic sugar, and the presence of all the linear algebra operations
I need (matrix multiplication, system resolution, cholesky...)
What are you using?
boost::array and also boost::MultiArray. There's also a pretty good linear algebra package in boost called uBLAS
There is also armadillo which I am using in some projects. From their website:
Armadillo is a C++ linear algebra library (matrix maths) aiming towards
a good balance between speed and ease
of use. Integer, floating point and
complex numbers are supported, as well
as a subset of trigonometric and
statistics functions. Various matrix
decompositions are provided through
optional integration with LAPACK and
ATLAS libraries.
A delayed evaluation approach is employed (during compile time) to
combine several operations into one
and reduce (or eliminate) the need for
temporaries. This is accomplished
through recursive templates and
template meta-programming.
This library is useful if C++ has been decided as the language of choice
(due to speed and/or integration
capabilities), rather than another
language like Matlab ® or Octave. It
is distributed under a license that is
useful in both open-source and
commercial contexts.
Armadillo is primarily developed at NICTA (Australia), with
contributions from around the world.
We've used TNT successfully for a number of years. There are sufficient issues, however, that we're moving toward an internally developed solution instead. The two biggest sticking points for us are that
The arrays are not thread safe, even for read access, because they use a non-thread safe reference count.
The arrays cause all sorts of problems when you write const-correct code.
If those aren't a problem then they're fairly convenient for a lot of common array tasks.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I've used
#include<stdlib>
#include<time>
using namespace std;
srand((unsigned)time(0));
int n=(rand()>>8)%4;
but what other random functions are there, or what other function could be used as random number generators?
EDIT: I don't really have a particular reason for asking this question, I just wanted to know if C++ had any other random functions.
Boost Random Number Library offers a broad range of generators (quality vs performance) and some typical random distributions. Everything rather nice and straightforward to use.
If you want some other methods/libraries - then google for cryptographic random numbers, also you can use this document as a reference.
Don't invent your own solutions unless you are an expert/researcher in the field/etc, take advantage of already existing solutions which were usually written by Smart People, and thoroughly examined by other Smart People.
The rand() and srand() functions are all the C++ Standard specifies. And if it comes to writing your own, be aware of what John von Neumann said:
"Anyone who considers arithmetical
methods of producing random digits is
of course in a state of sin"
This code is pretty efficient. Although users may begin to notice a pattern after a few iterations.
int FastRandom()
{
return 10;
}
Not strictly C++, but Windows specific:
CryptGenRandom
I'm sure all operating systems have their equivalent cryptographically secure random generator functions.
int unixrand()
{
int x;
int f = open("/dev/random", O_RDONLY);
if (f < 0) return -1; /* Error */
if (sizeof(x) != read(f, &x, sizeof(x))) {
close(f);
return -1;
}
close(f);
if (x < 0) x = ~x;
return x;
}
(Cross-posting from an answer I just wrote to a similar question)
Have a look at ISAAC (Indirection, Shift, Accumulate, Add, and Count). Its uniformly distributed and has an average cycle length of 2^8295.
It's fast too, since it doesnt involve multiplication or modulus.
Bruce Schneier and John Kelsey wrote a random number generator you may be interested in. Rather, it's a seed generator. Even though Yarrow is no longer supported, you may be interested in how it gathers entropy.
OpenSSL has an API that is relatively easy to access and pretty portable. And Mozilla comes with a decent API that wraps whatever the OS offers.
Personally, though, I generally use Boost.Random, which was already suggested.
Random gives you a good random number at uniform distribution and does a pretty good job at that.
Anything else would mean that you want to actually skew the distribution.
For example, using Microsoft's GUIDs generator would give you a random id that is less likely to be repeated and takes into account things like time and computer.
Time is usually the most random operation that is also cheap to perform, but it's still possible to predict.
If you want true randomness, using some kind of external input is your only solution.
Quantum Random Bit Generator is one service that provides such data.