Hello everyone, I have recently watched a tutorial on generating random numbers, however there seems to be a problem with my code which doesnt show up in the compiler of the person who did the tutorial.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(){
// An error appears here which states that "Implicit conversion loses integer precision: 'time_t' (aka 'long') to 'unsigned int' " This does not mean that the code canot be compiled however it does seem to cause a problem when the program is run.
srand(time(0));
// Random number between 10 and 20
cout << 10 + rand() % 11 << endl;
return 0;
}
The problem is that when I run the program, it starts at a random number between these bounds (which is great!) however when the program is run again, it takes the number immediately below that, and then again, and again, and when it reaches ten and is run again, then it just starts fgrom 20 and does exactly the same thing. So my output is looking like this:
17
16
15
14
13
12
11
10
20
19
18
17
16
and so on. Any help or feedback is greatly appreciated, thank you!
Thank you for all the feedback....
@dedlier Although with your method you still need to calculate limits e.g. ((rand()%11)+10; instead of your (rand()%20)+10; when I did slightly modify it, it worked perfectly in my compiler and generated truly (or very convincing) random numbers, unlike the previous method which I used. Oh and by the way, the person making the tutorial was doing it in codeblocks I think and that worked fine, however I am using xcode which comes up with an error with my original code, dont know why, but this works and just as easy to use! Thank you!
@geezle86, its not that I dont understand the concept (or atleast I think I do :) ) its just that my compiler was not giving the same output as the ones used on windows for some reason.