This question already has answers here:
Correctly setting random seeds for repeatability
(2 answers)
Random number generating in Fortran
(1 answer)
Closed last year.
I am confused about how random seed subroutine works in fortran. without any argument it takes the seed automatically from machine but I don't know how we can manually provide seed because we don't know what size it has. Also in some examples I have seen they just provide the variable for seed size and then write random_seed(get=seed), how this works? Does it takes seed automatically from machine
Related
This question already has answers here:
What common algorithms are used for C's rand()?
(3 answers)
Closed 2 years ago.
My question is straightforward. I know abour srand and rand, I know how to seed my random generator and so on. I am specifically interested in the mathematics behind srand. How does the computer use let's say 3333 as a seed and calculates a "random" number?
The c standard library's and therefore the c++ standard library's std::rand implementation is up to the library implementation. It is not mandated by the standard. The following function
(source: https://xkcd.com/221/)
would be a perfectly valid implementation for it.
That is the reason why it should under no circumstances be used any more. Instead defer to the well-defined, implementation independent and portable mersenne twister.
It depends on the library implementation but in general it would be enough to calculate a well mixed hash of the argument.
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 5 years ago.
Improve this question
Whenever we want to pick a random number from a vector we use a method called rand(). I want to know how it works from the backend.
rand has a seed value - e.g. setting it to the current time...
srand( time(NULL ) ); // second good enough
Then there is some math such as this....
unsigned int seed; // set by srand
unsigned int rand() {
seed = seed * number + offset;
return seed;
}
The number and offset are chosen, so the whole of the range of `unsigned int are covered. This generally means some form of prime number.
As mentioned in the comments, this is a very complex area.
If srand is not called, then seed has an initial value, which means that (ignoring thread timing issues), your program will get the same results each time it is run.
Getting the same results is handy for re-running tests, but problematic if it is say a game logic.
There is no "back-end" involved for rand.
BTW, in C++ you'll better use <random> standard header and related utilities, which are in the C++ standard library.
The rand function is part of the C standard library. It is unrelated to C++ vectors.
They (both the rand function and utilities from <random>) are based upon pseudo-random number generators, a quite complex field. You can still get a PhD by inventing better PRNGs.
If you want to understand how rand is (or can be) implemented, you'll better study the source code of some existing free software C standard library (like e.g. GNU glibc or musl-libc).
If you want to understand how <random> is implemented, study the source code of your C++ standard library. If you use the GCC compiler (e.g. compiling with the g++ program), it is provided by it.
This question already has answers here:
How to eliminate all sources of randomness so that program always gives identical answers?
(4 answers)
Closed 6 years ago.
I would like to know if I will get the same random numbers on all computers using the same srand() seed.
If not, how can I achieve that.
No. rand implementation is not standartized and different compiler vedors can and will use different algorithms.
You can use generators from C++11 <random> header which are standard and completely determenistic: mt19937 with same seed should give same sequence on all platforms, for example.
I am using the Armadillo c++ library, that allows high-perfomance computation of matrices and vectors. This library has built-in functions to populate its objects with random numbers. I use it in the context of a procedurial random generation of an object. The object creation is random, but no matter how often I recreate the object, it remains the same as long as the seed remains the same.
The issue is that, although I can set the seed to a determined value, and thus recreate the same run on my machine... I lose the coherence of the randomness when going to a different computer. I come from the enchanted land of Matlab where I can specify the function used for the generation of pseudo-random numbers. So, this generation can be cross platform if one chooses the function well. But how do I specify the RNG function for Armadillo?
My research has led me to this source documentation, that "detail" the process of random number generation:
http://arma.sourceforge.net/internal_docs_4300/a01181_source.html
http://arma.sourceforge.net/internal_docs_4300/a00087.html
But i have no clue on what to do here: this code is much more advanced than what I can write. I would appreciate any help!
Thank you guys!
Remarks:
- I do not care how good the random function used is. I just want a fast cross-platform cross-architecture generator. Deterministic randomness is my goal anyway.
- In details, in case it matters, the machines to consider should be intel processors, windows or mac, 32b or 64b.
- I have read the several posts mentionning the use of seeds for randomness but it seems that the problem here is the cross-platform context and the fact that the random generator is buried (to my untrained eyes at least) within Armadillo's code.
In C++98 / C++03 mode, Armadillo will internally use std::rand() for generating random numbers (there's more to it, but that's a good approximation of what's happening).
If you move from one operating system to the next (or across two versions of the same operating system), there is no guarantee that the system provided random number generator will be the same.
If you use Armadillo in C++11 mode, you can use any random number generator you like, with the help of the .imbue() function. Example:
std::mt19937 engine; // Mersenne twister random number engine with default parameters
std::uniform_real_distribution<double> distr(0.0, 1.0);
mat A(123,456);
A.imbue( [&]() { return distr(engine); } ); // fill with random numbers provided by the engine
The Mersenne twister random number engine is provided as standard functionality in C++11. The default parameters should be stable across compiler vendors and versions, and are independent of the operating system.
This question already has an answer here:
stl random distributions and portability
(1 answer)
Closed 8 years ago.
I'm generating a sequence of random numbers with std::mt19937_64. I've noticed that, when run with GCC and Clang on the same platform with the same seed, I obtain a different sequence. I've run the program through Valgrind and found no uninitialized memory.
Is there any guarantee to reproducibility across compilers or across platforms with std::mt19937_64?
Edit: Running with std::normal_distribution
The numbers that engines generate are guaranteed to be reproducible across implementations, but the distributions are not. (source: rand() considered harmful).
The N3337 standard draft says this about normal_distribution (26.5.8.5.1):
A normal_distribution random number distribution produces random numbers x distributed according to the probability density function
The distribution parameters µ and σ are also known as this distribution’s mean and standard deviation
And... that's it. It doesn't specify the order of the generated numbers, nor algorithm, nor example outputs.
The standard is very elaborate about mersenne_twister_engine (26.5.3.2), it specifies the state transition function, initial seeding algorithm and so on.