如何把生成随机数的代码改成线程??
我编了一个客户端程序,该程序按下“OnButton1”按钮之后,生成并发送15条指令(格式为“The application is r1 and the prior is r2”,其中r1,r2为随机数),代码如下(其中m_strEdit1为CString型的edit box的变量):
void CClientDlg::OnButton1()
{
UpdateData(TRUE);
int x[15],y[15];
CString str;
CString str1;
srand((unsigned)time(NULL));
int m;
for (m=0;m<15;m++)
{
x[m]=rand();
y[m]=x[m]%3+1;
str.Format("%d",x[m]);
str1.Format("%d.\r\n",y[m]);
m_strEdit1 = m_strEdit1 +"The application is "+ str+" and "
+"the prior is "+str1;
}
UpdateData(FALSE);
GetDlgItem(IDC_EDIT1)->RedrawWindow();
Sleep(1000);
OnDeal();
}
void CClientDlg::OnDeal()
{
int nLen;
int nSent;
UpdateData(TRUE);
if (!m_strEdit1.IsEmpty())
{
nLen=m_strEdit1.GetLength();
nSent=m_sConnectSocket.Send(LPCTSTR(m_strEdit1),nLen);
if (nSent==SOCKET_ERROR)
{
AfxMessageBox("信息发送错误!",MB_OK|MB_ICONSTOP);
}
m_strEdit1.Empty();
UpdateData(FALSE);
}
}
现在需要将其改成一个线程,这个线程的功能是不断生成并发送15条带随机数的指令,请问高手们,这个程序怎么改啊??
void CClientDlg::OnButton1()
{
UpdateData(TRUE);
int x[15],y[15];
CString str;
CString str1;
srand((unsigned)time(NULL));
int m;
for (m=0;m<15;m++)
{
x[m]=rand();
y[m]=x[m]%3+1;
str.Format("%d",x[m]);
str1.Format("%d.\r\n",y[m]);
m_strEdit1 = m_strEdit1 +"The application is "+ str+" and "
+"the prior is "+str1;
}
UpdateData(FALSE);
GetDlgItem(IDC_EDIT1)->RedrawWindow();
Sleep(1000);
OnDeal();
}
void CClientDlg::OnDeal()
{
int nLen;
int nSent;
UpdateData(TRUE);
if (!m_strEdit1.IsEmpty())
{
nLen=m_strEdit1.GetLength();
nSent=m_sConnectSocket.Send(LPCTSTR(m_strEdit1),nLen);
if (nSent==SOCKET_ERROR)
{
AfxMessageBox("信息发送错误!",MB_OK|MB_ICONSTOP);
}
m_strEdit1.Empty();
UpdateData(FALSE);
}
}
现在需要将其改成一个线程,这个线程的功能是不断生成并发送15条带随机数的指令,请问高手们,这个程序怎么改啊??
...全文
相关推荐
生成随机数 C#代码实现
生成随机数
c++随机数生成代码
既可以在不指定范围产生随机数 也可以在指定范围产生随机数,产生0到某个数的随机数
JAVA 生成随机数的3种方法
生成随机数 三个方法 java.util.Random class Math.random 方法,创建一个double类型的随机数 ThreadLocalRandom class 1) java.util.Random 要使用这个方法,首先要生成一个实例。来调用它的nextInt(),nextDouble(), nextLong() 等等 我们可以使用这个类来生成随机的 integer, float, double, long, booleans 我们可以传递一个参数到这个方法里,来设置一个范围的上限。例如,nextInt(6)将生成范围从0到5(包括0到5)的数字。 import jav
不使用Math.random方法生成随机数(随机数生成器)
不调用Math.random方法产生自己的随机数,现代计算机运行速度很快,在主线程等待一定毫秒数时,其他线程就会执行run方法中的while循环,一般会执行数十万次
随机数生成(C#)
多种获取随机数的比较,适合初学者与中级学者,方法,实例,源码,直接下载就可以使用。


进程/线程/DLL
