📄 random.cpp
字号:
#include "stdafx.h"
#include "random.h"
unsigned long CRandom::m_lNext = 1;
CRandom::CRandom() : m_hThread(NULL), m_dwThread(0), m_uRandom(0)
{
}
CRandom::~CRandom()
{
EndThread();
}
int CRandom::rand(void)
{
m_lNext = m_lNext * 1103515245 + 12345;
return (unsigned int)(m_lNext / (2 * (RAND_MAX+1L)) % (RAND_MAX+1L));
}
void CRandom::srand(unsigned int seed)
{
m_lNext = seed;
}
unsigned long CRandom::GetRandom()
{
return m_uRandom + rand();
}
bool CRandom::StartThread()
{
if(m_hThread != NULL)
return false;
srand(time(NULL));
m_hThread = (HANDLE)_beginthreadex
(
(void *)NULL,
(unsigned)0,
(PTHREAD_START)RandomThread,
(void *)this,
(unsigned)0,
(unsigned *)&m_dwThread
);
if(m_hThread == NULL)
return false;
return true;
}
void CRandom::EndThread()
{
if(m_hThread)
{
PostThreadMessage(m_dwThread, WM_USER+100, 0, 0);
WaitForSingleObject(m_hThread, 500);
CloseHandle(m_hThread);
m_hThread = NULL;
}
m_dwThread = 0;
}
DWORD WINAPI CRandom::RandomThread(void *pvParam)
{
MSG msg;
CRandom *pRandom = (CRandom *)pvParam;
DWORD dwSleep;
while(1)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_USER+100)
break;
}
dwSleep = 10 + pRandom->rand() % 150;
Sleep(dwSleep);
pRandom->m_uRandom = (unsigned long)pRandom->rand();
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -