I everybody, I'm searching for a method to use rand()and srand() to have a randomic number between -x and x.
x is a normal double variable... how can I solv it?
I sow that in C you can use seed...
Does it need to be using rand(), or are you only saying that because that's what you're familiar with?
I second using uniform_real_distribution, it's much easier to work with, despite the slightly more complicated setup.
See the example in keskiverto's link.
std::random device is more robust than rand for most trivial cases and is as easy to use when instantiated. No need to seed the bugger.
The C++ random engines and distributions look "scary" because they offer a lot more options vs. rand. Most programming courses don't cover <random> (or <chrono>).
Hi everybody, thanks all for the answers.
I should do it only for university homework so it isn't "important"...
I'm a beginner and I want to make a program where a variable takes a value from a random number between -x and x.
So if I have double x = 5.0;
What I want is double y = rand();
And y should have a value between -5.0 and 5.0 .... so -4.2 || 0.0 || 3.8 ecc...
My university is currently teaching C++14, but they allow me to submit code written in C++17 or some C++20 features since they are running the latest version of g++ provided I give the exact compilation instructions or makefile
That is not the norm for most schools or universities. The curriculum is very often years out of date, usually taught by people who were trained by a curriculum just as old. If not older.