作业帮 > 综合 > 作业

在C#中产生随机数的函数及用法?

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/04/30 14:15:22
在C#中产生随机数的函数及用法?
想在C#中实现产生多个线程,并使每个线程产生随机数
给你的完整的,以前做的
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime;
using System.Threading;
namespace RandomNumber
{
///
/// Form1 的摘要说明.
///
public class FrmMain : System.Windows.Forms.Form
{
//private Point mouseOffset;
//Random ran=new Random();
private bool isMoseDown;
private bool IfThreadStart;
Thread t1,t2,t3,t4,t5,t6;
private System.Windows.Forms.Label L1;
private System.Windows.Forms.Label L2;
private System.Windows.Forms.Label L3;
private System.Windows.Forms.Label L4;
private System.Windows.Forms.Label L5;
private System.Windows.Forms.Label L6;
private System.Windows.Forms.Label lblLast;
private System.Windows.Forms.PictureBox btnExit;
private System.Windows.Forms.PictureBox btnStart;

///
/// 必需的设计器变量.
///
private System.ComponentModel.Container components = null;
public FrmMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容.
///
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FrmMain));
this.L1 = new System.Windows.Forms.Label();
this.L2 = new System.Windows.Forms.Label();
this.L3 = new System.Windows.Forms.Label();
this.L4 = new System.Windows.Forms.Label();
this.L5 = new System.Windows.Forms.Label();
this.L6 = new System.Windows.Forms.Label();
this.lblLast = new System.Windows.Forms.Label();
this.btnExit = new System.Windows.Forms.PictureBox();
this.btnStart = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// L1
//
this.L1.BackColor = System.Drawing.Color.Yellow;
this.L1.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.L1.Location = new System.Drawing.Point(112, 168);
this.L1.Name = "L1";
this.L1.Size = new System.Drawing.Size(24, 24);
this.L1.TabIndex = 0;
this.L1.Text = "1";
this.L1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// L2
//
this.L2.BackColor = System.Drawing.Color.Yellow;
this.L2.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.L2.Location = new System.Drawing.Point(176, 168);
this.L2.Name = "L2";
this.L2.Size = new System.Drawing.Size(24, 24);
this.L2.TabIndex = 0;
this.L2.Text = "2";
this.L2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// L3
//
this.L3.BackColor = System.Drawing.Color.Yellow;
this.L3.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.L3.Location = new System.Drawing.Point(248, 168);
this.L3.Name = "L3";
this.L3.Size = new System.Drawing.Size(24, 24);
this.L3.TabIndex = 0;
this.L3.Text = "3";
this.L3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// L4
//
this.L4.BackColor = System.Drawing.Color.Yellow;
this.L4.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.L4.Location = new System.Drawing.Point(312, 168);
this.L4.Name = "L4";
this.L4.Size = new System.Drawing.Size(24, 24);
this.L4.TabIndex = 0;
this.L4.Text = "4";
this.L4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// L5
//
this.L5.BackColor = System.Drawing.Color.Yellow;
this.L5.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.L5.Location = new System.Drawing.Point(384, 168);
this.L5.Name = "L5";
this.L5.Size = new System.Drawing.Size(24, 24);
this.L5.TabIndex = 0;
this.L5.Text = "5";
this.L5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// L6
//
this.L6.BackColor = System.Drawing.Color.Yellow;
this.L6.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.L6.Location = new System.Drawing.Point(448, 168);
this.L6.Name = "L6";
this.L6.Size = new System.Drawing.Size(24, 24);
this.L6.TabIndex = 0;
this.L6.Text = "6";
this.L6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// lblLast
//
this.lblLast.BackColor = System.Drawing.Color.Yellow;
this.lblLast.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.lblLast.Location = new System.Drawing.Point(208, 32);
this.lblLast.Name = "lblLast";
this.lblLast.Size = new System.Drawing.Size(288, 48);
this.lblLast.TabIndex = 1;
this.lblLast.Text = "中奖号码为:123456";
this.lblLast.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

//
// btnExit
//
this.btnExit.Image = ((System.Drawing.Image)(resources.GetObject("btnExit.Image")));
this.btnExit.Location = new System.Drawing.Point(75, 11);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(16, 16);
this.btnExit.TabIndex = 2;
this.btnExit.TabStop = false;
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// btnStart
//
this.btnStart.Image = ((System.Drawing.Image)(resources.GetObject("btnStart.Image")));
this.btnStart.Location = new System.Drawing.Point(7, 80);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(16, 16);
this.btnStart.TabIndex = 3;
this.btnStart.TabStop = false;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// FrmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(504, 216);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.lblLast);
this.Controls.Add(this.L1);
this.Controls.Add(this.L2);
this.Controls.Add(this.L3);
this.Controls.Add(this.L4);
this.Controls.Add(this.L5);
this.Controls.Add(this.L6);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.KeyPreview = true;
this.Name = "FrmMain";
this.TransparencyKey = System.Drawing.Color.Yellow;
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.FrmMain_MouseDown);
this.Load += new System.EventHandler(this.FrmMain_Load);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.FrmMain_MouseUp);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.FrmMain_MouseMove);
this.ResumeLayout(false);
}
#endregion
///
/// 应用程序的主入口点.
///
[STAThread]
static void Main()
{
Application.Run(new FrmMain());
}
private void FrmMain_Load(object sender, System.EventArgs e)
{
this.IfThreadStart=false;
}
private void FrmMain_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
isMoseDown=true;
}
}
private void FrmMain_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(isMoseDown)
{
this.Location=Control.MousePosition;
}
}
private void FrmMain_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
this.isMoseDown=false;
}
}
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
Application.Exit();
}
private void btnStart_Click(object sender, System.EventArgs e)
{
if(IfThreadStart==false)
{
lblLast.Visible=false;
InThread Th1=new InThread();
Th1.SetNumber(L1,111);
t1= new Thread(new ThreadStart(Th1.RunThread));
t1.Start();
InThread Th2=new InThread();
Th2.SetNumber(L2,22222);
t2= new Thread(new ThreadStart(Th2.RunThread));
t2.Start();
InThread Th3=new InThread();
Th3.SetNumber(L3,3333);
t3= new Thread(new ThreadStart(Th3.RunThread));
t3.Start();
InThread Th4=new InThread();
Th4.SetNumber(L4,100000);
t4= new Thread(new ThreadStart(Th4.RunThread));
t4.Start();
InThread Th5=new InThread();
Th5.SetNumber(L5,99);
t5=new Thread(new ThreadStart(Th5.RunThread));
t5.Start();
InThread Th6=new InThread();
Th6.SetNumber(L6,8);
t6= new Thread(new ThreadStart(Th6.RunThread));
t6.Start();
this.IfThreadStart=true;

}
else
{
t1.Abort();
t2.Abort();
t3.Abort();
t4.Abort();
t5.Abort();
t6.Abort();
this.IfThreadStart=false;//设置下次按键是有效
lblLast.Text="中奖号码为:"+L1.Text+L2.Text+L3.Text+L4.Text+L5.Text+L6.Text;
lblLast.Visible=true;
}
}


}
class InThread
{
private Control A;
private int seed;
public void RunThread()
{
Random ran=new Random(seed);
do
{
A.Text=ran.Next(0,10).ToString();
Thread.Sleep(100);
}
while(true);
}
public void SetNumber(Control B,int sd)
{
A=B;
seed=sd;
}
}
}
再问: 程序运行不过去啊,出现错误 CS0234: 命名空间“System”中不存在类型或命名空间名称“Drawing”。是否缺少程序集引用?
再答: 你自己家个断点看看!要学会分析代码的能力