how would i generate a seed or hash that would make rand actually random? I need it to change every time it picks a number. New to c++ so i'm not exactly sure how to do this. Thanks! :D
If you want to use RAND to generate a random number but don't want the numbers to change every time the cell is calculated, you can enter =RAND() in the formula bar, and then press F9 to change the formula to a random number.
Python Random seed() Method The random number generator needs a number to start with (a seed value), to be able to generate a random number. By default the random number generator uses the current system time. Use the seed() method to customize the start number of the random number generator.
However, the numbers generated by the rand() are not random because it generates the same sequence each time the code executed.
This is because MATLAB's random number generator is initialized to the same state each time MATLAB starts up. If you wish to generate different random values in each MATLAB session, you can use the system clock to initialize the random number generator once at the beginning of each MATLAB session.
With C++11 you can use std::random_device
. I would suggest you to watch link for a comprehensive guide.
Extracting the essential message from the video link : You should never use srand
& rand
, but instead use std::random_device
and std::mt19937
-- for most cases, the following would be what you want:
#include <iostream>
#include <random>
int main() {
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist(0,99);
for (int i = 0; i < 16; i++) {
std::cout << dist(mt) << " ";
}
std::cout << std::endl;
}
There is no such thing as an "actually random" random number generator without sampling environmental data or accessing a quantum random number source. Consider accessing the ANU random number source if you require truly random numbers (http://qrng.anu.edu.au/FAQ.php#api).
Otherwise, Boost provides a more robust pseudo-RNG, which should suffice for most purposes: http://www.boost.org/doc/libs/1_58_0/doc/html/boost_random.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With