Rand generating same numbers

前端 未结 3 723
粉色の甜心
粉色の甜心 2021-01-21 11:52

I have a problem with the small game that I made.

#include \"stdafx.h\"
#include 
#include 
#include 
using namespace         


        
3条回答
  •  天涯浪人
    2021-01-21 11:57

    There are a number of problems with rand(). You've run into one of them, which is that the first several values aren't "random". If you must use rand(), it is always a good idea to discard the first four or results from rand().

    srand (time(0));
    rand();
    rand();
    rand();
    rand();
    

    Another problem with rand() is that the low order bits are notoriously non-random, even after the above hack. On some systems, the lowest order bit alternates 0,1,0,1,0,1,... It's always better to use the high order bits such as by using the quotient rather than the remainder.

    Other problems: Non-randomness (most implementations of rand() fails a number of tests of randomness) and short cycle. With all these problems, the best advice is to use anything but rand().

提交回复
热议问题