rand() returns the same number each time the program is run

后端 未结 6 1677
自闭症患者
自闭症患者 2021-02-02 10:46

In this rather basic C++ code snippet involving random number generation:

include 
using namespace std;

int main() {
    cout << (rand() %         


        
相关标签:
6条回答
  • 2021-02-02 11:21

    For what its worth you are also only generating numbers between 0 and 99 (inclusive). If you wanted to generate values between 0 and 100 you would need.

    rand() % 101
    

    in addition to calling srand() as mentioned by others.

    0 讨论(0)
  • 2021-02-02 11:24

    You need to change the seed.

    int main() {
    
        srand(time(NULL));
        cout << (rand() % 101);
        return 0;
    }
    

    the srand seeding thing is true also for a c language code.


    See also: http://xkcd.com/221/

    0 讨论(0)
  • 2021-02-02 11:26

    You are not seeding the number.

    Use This:

    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
        srand(static_cast<unsigned int>(time(0)));
        cout << (rand() % 100) << endl;
        return 0;
    }
    

    You only need to seed it once though. Basically don't seed it every random number.

    0 讨论(0)
  • 2021-02-02 11:27

    You need to "seed" the generator. Check out this short video, it will clear things up.

    https://www.thenewboston.com/videos.php?cat=16&video=17503

    0 讨论(0)
  • random functions like borland complier

    using namespace std;
    
    int sys_random(int min, int max) {
       return (rand() % (max - min+1) + min);
    }
    
    void sys_randomize() {
        srand(time(0));
    }
    
    0 讨论(0)
  • 2021-02-02 11:31

    srand() seeds the random number generator. Without a seed, the generator is unable to generate the numbers you are looking for. As long as one's need for random numbers is not security-critical (e.g. any sort of cryptography), common practice is to use the system time as a seed by using the time() function from the <ctime> library as such: srand(time(0)). This will seed the random number generator with the system time expressed as a Unix timestamp (i.e. the number of seconds since the date 1/1/1970). You can then use rand() to generate a pseudo-random number.

    Here is a quote from a duplicate question:

    The reason is that a random number generated from the rand() function isn't actually random. It simply is a transformation. Wikipedia gives a better explanation of the meaning of pseudorandom number generator: deterministic random bit generator. Every time you call rand() it takes the seed and/or the last random number(s) generated (the C standard doesn't specify the algorithm used, though C++11 has facilities for specifying some popular algorithms), runs a mathematical operation on those numbers, and returns the result. So if the seed state is the same each time (as it is if you don't call srand with a truly random number), then you will always get the same 'random' numbers out.

    If you want to know more, you can read the following:

    http://www.dreamincode.net/forums/topic/24225-random-number-generation-102/

    http://www.dreamincode.net/forums/topic/29294-making-pseudo-random-number-generators-more-random/

    0 讨论(0)
提交回复
热议问题