| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 895 人关注过本帖
标题:Don't quite understand rand().
只看楼主 加入收藏
点线面
Rank: 8Rank: 8
来 自:NO.-1
等 级:蝙蝠侠
帖 子:525
专家分:980
注 册:2011-1-3
结帖率:100%
收藏
 问题点数:0 回复次数:1 
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.
搜索更多相关主题的帖子: always 
2011-01-12 15:02
天蝎殿
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2011-8-22
收藏
得分:0 
before you use rand()
seed it with the function srand(static_cast<unsigned int>(time(0));

also the function time need the below header:
#include <ctime>
2011-08-22 13:08
快速回复:Don't quite understand rand().
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016934 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved