Hello everybody I'm trying to pick up c++ with some selfhelp books and youtube tutorials, and ran into a problem where double num1 - num1 = rand() - cout num1 keeps yielding the result 41..
The code below is from a youtube tutorial named "C++ Tutorial - 7 - Arguments in Functions" (without the "" stuff..).
I hope someone can bonk me on the head and tell me why it makes sense it keeps returning 41 all the time (no matter how many times I run it or recompile it)
You need to initialize the pseudo-random number generator once at the beginning of your program.
When you use the same value to initialize it, you'll always get the same sequence of numbers, so it's a good idea to use the current time: srand(time(0));
Aside from that, it's a bad idea to try to learn a programming language using video tutorials.
There's nothing to show except code which can be done a hundred times better using text instead of a video.
And the worst part is that most of the people creating these video tutorials are completely inexperienced themselves and will tell you a bunch of things that are just plain wrong.
So you should get yourself a good book. This will save you a lot of time and headache later on.
(semi philosophical) side question if anyone's up for it / sees it:
Manifesting the conceptualization of randomness is a deterministic approach to indeterminism isnt it? (or some mumbo jumbo like that) - is it existentially possible to determine indeterminism? And if so, is it possible with the numbers/values available in the world of programming?....
edit: ps. - yea I know that the sources no matter what material you have at hand for whatever you're doing always need to be questioned as much as possible ;P
Yeah, pseudo-random number generators are always deterministic (hence the name), but since the numbers appear to be random, they're fine for most purposes.
There are other approaches for when you need real random numbers. In Linux, you can read from /dev/urandom, which will get you something closer to real random numbers, as unpredictable things like noise from hardware components are incorporated. The Windows Crypto API likely uses similar techniques.
There are also hardware random numbers generators that rely on quantum processes to generate completely unpredictable numbers.