Don't quite understand rand().

I'm new to C++ and am following a guide and one of the exercises is to make a magic number guessing game. You use rand() to generate a random number but when i run the program the number is always 41. Can some help explain? Here's the program...

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Magic Number program.

#include <iostream>
#include <cstdlib>
#include <limits>
using namespace std;

int main()
{
	int magic; // magic number
	int guess; // user's guess

	magic = rand(); // get a random number

	do
	{
		cout<<"Enter your guess: ";
		cin>>guess;

		if(guess == magic) 
		{
			cout <<"** Right **\n";
			cout<<magic<<" is the magic number.\n";
		}
		else
		{
			cout<<"...Sorry, you're wrong.\n"; 
			if(guess > magic) 
				cout <<"Your guess is too high.\n";
			else cout<<"Your guess is too low.\n";
		}
	}	while(guess != magic);
	
	cin.sync();
	cout << "Press ENTER to continue...";
	cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

	return 0;
}

rand will generate the same sequence of number given the same seed.
Usually, you call srand(time(0)); to get a somewhat random seed for the pseudo-random sequence generated by rand
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
i really shouldnt answer this its more of a guess but random is not truley random. you have to seed it for different results.

*edit bazzy beat me by a minute. i blame my typing.
Last edited on
As an aside, the mathematical contortions behind chasing the random for purposes such as cryptography are pretty involved and rather interesting, if that's your cup of tea.
cryptography is pretty cool until you realize all the algorithms for it are copyright and not free to use in your programs. so its fun to learn but in the end kinda pointless because you cant use em. i wanted to create a crypt lib until i investigated more.
Perhaps you could make use of one of the many open source cryptography libraries, in which the algorithms are freely available for you to make use of.

For example, gnupg or libgcrypt, covered by the GPL, or OpenSSL, covered by its own licence which permits redistribution and use of both binary and source, with various conditions that in no way stop you using them in your own programs.

Long story short, I think I'm saying that it is incorrect to state that all crypto algorithms are copyright and not free to use in your programs; there's lots out there you can play with. Here are some more words on the subject:

http://security.stackexchange.com/questions/1300/copyright-issues-with-encryption-algorithms

My understanding of the issue, which of course applies only in some legal regimes, is that copyright applies only to a specific, copyable expression of an idea (the source code, for example), not the idea itself (the actual concept of how that crypto works). Any number of people can express the same algorithm in different ways, and when they commit those expressions to some tangible form such as a program, every one of them is separate for purposes of copyright.
Last edited on
So basically copywrites disallow "copy, paste, this is mine!"
That's certainly more reasonable than what I thought they did.
Topic archived. No new replies allowed.