Trouble about Random Numbers

So I am a beginner programmer learning about to program, and I have a question in particular about random numbers. I am basing me code of the book for my school. After reading it, the code tells me I should do the code like this. The problem with this is that every time I compile it, it just does not seem random to me and it goes up by 3 every time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	
	unsigned seed = time(0);

	srand(seed);
	const int MIN_VALUE = 1;
	const int MAX_VALUE = 100;

	cout << (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE << endl;

	
	return 0;
}


Then after specifically following me book that is required for my course, I decided to do this followed by what the book told me. It still goes up by 3 and does not seem very random to me!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	unsigned seed = time(0);
	srand(seed);
	
	const int MIN_VALUE = 1;
	const int MAX_VALUE = 100;
	double y;
	y = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;

	cout << y << endl;
	
	
	return 0;
}


Then after countless of minutes, I tried to play around with it and I got the solution for true randomness, but I have no idea why it works. I am literally defining it twice, but in the book it goes it once. Thank you all for reading and hope to get fast responses! :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	unsigned seed = time(0);
	srand(seed);
	
	const int MIN_VALUE = 1;
	const int MAX_VALUE = 100;
	double y;
	y = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;

	cout << (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE << endl;
	
	return 0;
}
std::rand() is usually a poor quality random number generator. When the program is run repeatedly, the value returned by time(0); tends to go up in predictable increments, and this may be reflected in the result of std::rand()

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <thread>
#include <chrono>

int main()
{
    for( int i = 1 ; i < 10 ; ++i )
    {
        const unsigned int seed = std::time(0);
        std::srand(seed);

        const int MIN_VALUE = 1;
        const int MAX_VALUE = 100;
        int y = std::rand() % (MAX_VALUE - MIN_VALUE + 1) + MIN_VALUE;

        std::cout << seed << ' ' << y  << '\n' << std::flush ;
        
        // simulate running the program after every second
        std::this_thread::sleep_for( std::chrono::seconds(1) ) ;
    }
}

1497669485 21
1497669486 24
1497669487 28
1497669488 31
1497669489 34
1497669490 38
1497669491 41
1497669493 47
1497669494 51

http://rextester.com/FQDTS48795



C++ has better pseudo random number engines, in header <random>
http://en.cppreference.com/w/cpp/numeric/random

For instance:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <ctime>
#include <random>
#include <thread>
#include <chrono>

int main()
{
    for( int i = 1 ; i < 10 ; ++i )
    {
        const int seed = std::time(nullptr) ;
        std::mt19937 rng(seed) ;
    
        const int MIN_VALUE = 1;
        const int MAX_VALUE = 100;
        std::uniform_int_distribution<int> distr( MIN_VALUE, MAX_VALUE ) ;
    
        const int y = distr(rng) ;

        std::cout << seed << ' ' << y  << '\n' << std::flush ;
        
        // simulate running the program after every second
        std::this_thread::sleep_for( std::chrono::seconds(1) ) ;
    }
}

1497669263 89
1497669264 70
1497669265 72
1497669266 100
1497669267 75
1497669268 38
1497669269 94
1497669270 57
1497669271 56

http://rextester.com/JWJMV63327
Thanks you, my question has been answered. I will mark as resolved. :)
Topic archived. No new replies allowed.