如果在很短的时间内连接获取随机数,会发现获取的随机数是同一个,下面例子可以实现短时间内快速获取不一样的随机数:

 

#include <windows.h>
#include <iostream>
using namespace std;

//#define USE_QUICK_RAND

DWORD WINAPI thread(PVOID pBuf)
{

    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    srand(li.QuadPart);
    cout<<"random number: "<<rand();

    return 0;
}

void main()
{
    for (int i = 0; i < 10; i++)
    {
#ifdef USE_QUICK_RAND
        LARGE_INTEGER li;
        QueryPerformanceCounter(&li);
        srand(li.QuadPart);
#else
        srand(GetTickCount());
#endif
        cout<<"random number: "<<rand()<<endl;
    }
}

相关文章:

  • 2021-07-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-16
  • 2022-12-23
  • 2022-01-03
  • 2021-11-27
猜你喜欢
  • 2022-12-23
  • 2022-03-08
  • 2022-12-23
  • 2021-08-17
  • 2022-12-23
  • 2021-10-23
  • 2021-10-14
相关资源
相似解决方案