Don't quite understand rand().
crazzzyhands (1) Jan 12, 2011 at 6:30am 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
我是新学C++,我抄书本上的制造魔术数字的例子
() to generate a random number but when i run the program the number is always 41. Can some help explain? Here's the program...
想生成一个随机数字,不过程序总是输出41,某些人帮助我解悉
// 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;
}
Bazzy (5599) Jan 12, 2011 at 6:38am
rand will generate the same sequence of number given the same seed.
rand只会生成同一样数字
Usually, you call srand(time(0)); to get a somewhat random seed for the pseudo-
通常,你调用srand(time(0)) ;再用rand得到伪随机种子
random sequence generated by rand
http://www.
http://www.
acorn (22) Jan 12, 2011 at 6:39am
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 Jan 12, 2011 at 6:40am
Moschops (42) Jan 12, 2011 at 6:42am
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.
值得一提,如果密码都可以随机,那就相当有趣,吊人胃口
acorn (22) Jan 12, 2011 at 6:45am
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.
Moschops (42) Jan 12, 2011 at 7:19am
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.
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 Jan 12, 2011 at 7:28am
Browni3141 (167) Jan 12, 2011 at 9:21am
So basically copywrites disallow "copy, paste, this is mine!"
That's certainly more reasonable than what I thought they did.