分享
 
 
 

C#随机数的应用[原创]

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

C#随机数的应用[原创]

自己动手用C#编写体育彩票

现在有很多彩票软件神乎其神,但归根到底都是随机数的使用。我们用6位随机数来演示在C#中使用随机数

从而自己制作自己的彩票软件:)

--------------------------------

代码如下

/* 这是一个简单的随机数使用

* 2004年3月29日

*/

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace MyLotteryProject

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class MyLotteryForm : System.Windows.Forms.Form

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.Windows.Forms.TextBox textBox1;

private System.Windows.Forms.TextBox textBox2;

private System.Windows.Forms.TextBox textBox3;

private System.Windows.Forms.TextBox textBox4;

private System.Windows.Forms.TextBox textBox5;

private System.Windows.Forms.TextBox textBox6;

public static int n_draw=1;

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label2;

private System.Windows.Forms.Label labelDaytime;

private System.Windows.Forms.Label label3;

private System.Windows.Forms.Label label4;

private System.Windows.Forms.Label label5;

public int[] lottonumbers = new int[50]; //够放6个随机数字吧

public int[] result = new int[50];

private System.Windows.Forms.Button pbSort;

private System.Windows.Forms.Button pbEnd;

private System.Windows.Forms.Button pbNextDraw;

private System.Windows.Forms.Label labelSort;

private System.Windows.Forms.PictureBox pictureBox1;

private System.Windows.Forms.PictureBox pictureBox3;

private System.Windows.Forms.PictureBox pictureBox4;

private System.Windows.Forms.Label label6;

private System.ComponentModel.Container components = null;

public MyLotteryForm()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

System.Resources.ResourceManager resources = new

System.Resources.ResourceManager(typeof(MyLotteryForm));

this.textBox1 = new System.Windows.Forms.TextBox();

this.textBox2 = new System.Windows.Forms.TextBox();

this.textBox3 = new System.Windows.Forms.TextBox();

this.textBox4 = new System.Windows.Forms.TextBox();

this.textBox5 = new System.Windows.Forms.TextBox();

this.textBox6 = new System.Windows.Forms.TextBox();

this.pbNextDraw = new System.Windows.Forms.Button();

this.label1 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.labelDaytime = new System.Windows.Forms.Label();

this.label3 = new System.Windows.Forms.Label();

this.label4 = new System.Windows.Forms.Label();

this.pbEnd = new System.Windows.Forms.Button();

this.label5 = new System.Windows.Forms.Label();

this.pbSort = new System.Windows.Forms.Button();

this.labelSort = new System.Windows.Forms.Label();

this.pictureBox1 = new System.Windows.Forms.PictureBox();

this.pictureBox3 = new System.Windows.Forms.PictureBox();

this.pictureBox4 = new System.Windows.Forms.PictureBox();

this.label6 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// textBox1

//

this.textBox1.BackColor = System.Drawing.Color.Magenta;

this.textBox1.Location = new System.Drawing.Point(106, 190);

this.textBox1.Name = "textBox1";

this.textBox1.ReadOnly = true;

this.textBox1.Size = new System.Drawing.Size(28, 21);

this.textBox1.TabIndex = 0;

this.textBox1.Text = "textBox1";

//

// textBox2

//

this.textBox2.BackColor = System.Drawing.Color.Gold;

this.textBox2.Location = new System.Drawing.Point(144, 190);

this.textBox2.Name = "textBox2";

this.textBox2.ReadOnly = true;

this.textBox2.Size = new System.Drawing.Size(29, 21);

this.textBox2.TabIndex = 1;

this.textBox2.Text = "textBox2";

//

// textBox3

//

this.textBox3.BackColor = System.Drawing.Color.Cyan;

this.textBox3.Location = new System.Drawing.Point(182, 190);

this.textBox3.Name = "textBox3";

this.textBox3.ReadOnly = true;

this.textBox3.Size = new System.Drawing.Size(29, 21);

this.textBox3.TabIndex = 2;

this.textBox3.Text = "textBox3";

//

// textBox4

//

this.textBox4.BackColor = System.Drawing.Color.Red;

this.textBox4.Location = new System.Drawing.Point(221, 190);

this.textBox4.Name = "textBox4";

this.textBox4.ReadOnly = true;

this.textBox4.Size = new System.Drawing.Size(29, 21);

this.textBox4.TabIndex = 3;

this.textBox4.Text = "textBox4";

//

// textBox5

//

this.textBox5.BackColor = System.Drawing.Color.Yellow;

this.textBox5.Location = new System.Drawing.Point(259, 190);

this.textBox5.Name = "textBox5";

this.textBox5.ReadOnly = true;

this.textBox5.Size = new System.Drawing.Size(29, 21);

this.textBox5.TabIndex = 4;

this.textBox5.Text = "textBox5";

//

// textBox6

//

this.textBox6.BackColor = System.Drawing.Color.Lime;

this.textBox6.Location = new System.Drawing.Point(298, 190);

this.textBox6.Name = "textBox6";

this.textBox6.ReadOnly = true;

this.textBox6.Size = new System.Drawing.Size(28, 21);

this.textBox6.TabIndex = 5;

this.textBox6.Text = "textBox6";

//

// pbNextDraw

//

this.pbNextDraw.BackColor = System.Drawing.Color.PaleGoldenrod;

this.pbNextDraw.Cursor = System.Windows.Forms.Cursors.Hand;

this.pbNextDraw.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

this.pbNextDraw.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,

System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.pbNextDraw.ForeColor = System.Drawing.Color.Goldenrod;

this.pbNextDraw.Location = new System.Drawing.Point(10, 250);

this.pbNextDraw.Name = "pbNextDraw";

this.pbNextDraw.Size = new System.Drawing.Size(211, 43);

this.pbNextDraw.TabIndex = 6;

this.pbNextDraw.Text = "下一注 >";

this.pbNextDraw.Click += new System.EventHandler(this.pbNextDraw_Click);

//

// label1

//

this.label1.BackColor = System.Drawing.Color.Coral;

this.label1.Font = new System.Drawing.Font("Comic Sans MS", 18F,

System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.label1.Location = new System.Drawing.Point(38, 17);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(384, 43);

this.label1.TabIndex = 7;

this.label1.Text = "infofox.org彩票软件 .net0.01V";

//

// label2

//

this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,

System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.label2.ForeColor = System.Drawing.Color.IndianRed;

this.label2.Location = new System.Drawing.Point(29, 129);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(211, 25);

this.label2.TabIndex = 8;

this.label2.Text = "买6个数字肯定可以中奖";

//

// labelDaytime

//

this.labelDaytime.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,

System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.labelDaytime.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)),

((System.Byte)(192)), ((System.Byte)(0)));

this.labelDaytime.Location = new System.Drawing.Point(250, 129);

this.labelDaytime.Name = "labelDaytime";

this.labelDaytime.Size = new System.Drawing.Size(192, 26);

this.labelDaytime.TabIndex = 9;

this.labelDaytime.Click += new System.EventHandler(this.labelDaytime_Click);

//

// label3

//

this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,

System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.label3.Location = new System.Drawing.Point(29, 86);

this.label3.Name = "label3";

this.label3.Size = new System.Drawing.Size(43, 25);

this.label3.TabIndex = 10;

this.label3.Text = "第";

this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

this.label3.Click += new System.EventHandler(this.label3_Click);

//

// label4

//

this.label4.BackColor = System.Drawing.Color.Bisque;

this.label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,

System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.label4.ForeColor = System.Drawing.Color.Red;

this.label4.Location = new System.Drawing.Point(106, 86);

this.label4.Name = "label4";

this.label4.Size = new System.Drawing.Size(48, 26);

this.label4.TabIndex = 11;

this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

//

// pbEnd

//

this.pbEnd.BackColor = System.Drawing.SystemColors.Control;

this.pbEnd.Cursor = System.Windows.Forms.Cursors.Hand;

this.pbEnd.ForeColor = System.Drawing.SystemColors.ControlText;

this.pbEnd.Location = new System.Drawing.Point(355, 258);

this.pbEnd.Name = "pbEnd";

this.pbEnd.Size = new System.Drawing.Size(96, 25);

this.pbEnd.TabIndex = 12;

this.pbEnd.Text = "退出";

this.pbEnd.Click += new System.EventHandler(this.pbEnd_Click);

//

// label5

//

this.label5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

this.label5.Font = new System.Drawing.Font("Times New Roman", 14F,

System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.label5.Location = new System.Drawing.Point(0, 310);

this.label5.Name = "label5";

this.label5.Size = new System.Drawing.Size(470, 25);

this.label5.TabIndex = 13;

this.label5.Text = "您中了头奖- 别忘了我啊http://infofox.org";

//

// pbSort

//

this.pbSort.BackColor = System.Drawing.SystemColors.Control;

this.pbSort.ForeColor = System.Drawing.SystemColors.ControlText;

this.pbSort.Location = new System.Drawing.Point(240, 258);

this.pbSort.Name = "pbSort";

this.pbSort.Size = new System.Drawing.Size(96, 25);

this.pbSort.TabIndex = 14;

this.pbSort.Text = " 开始计算";

this.pbSort.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

this.pbSort.Click += new System.EventHandler(this.pbSort_Click);

//

// labelSort

//

this.labelSort.Location = new System.Drawing.Point(38, 190);

this.labelSort.Name = "labelSort";

this.labelSort.Size = new System.Drawing.Size(58, 24);

this.labelSort.TabIndex = 15;

this.labelSort.Text = "Sorted:";

this.labelSort.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

//

// pictureBox1

//

this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject

("pictureBox1.Image")));

this.pictureBox1.Location = new System.Drawing.Point(248, 72);

this.pictureBox1.Name = "pictureBox1";

this.pictureBox1.Size = new System.Drawing.Size(40, 40);

this.pictureBox1.TabIndex = 16;

this.pictureBox1.TabStop = false;

//

// pictureBox3

//

this.pictureBox3.Image = ((System.Drawing.Image)(resources.GetObject

("pictureBox3.Image")));

this.pictureBox3.Location = new System.Drawing.Point(312, 72);

this.pictureBox3.Name = "pictureBox3";

this.pictureBox3.Size = new System.Drawing.Size(40, 40);

this.pictureBox3.TabIndex = 18;

this.pictureBox3.TabStop = false;

//

// pictureBox4

//

this.pictureBox4.Image = ((System.Drawing.Image)(resources.GetObject

("pictureBox4.Image")));

this.pictureBox4.Location = new System.Drawing.Point(376, 72);

this.pictureBox4.Name = "pictureBox4";

this.pictureBox4.Size = new System.Drawing.Size(40, 40);

this.pictureBox4.TabIndex = 19;

this.pictureBox4.TabStop = false;

//

// label6

//

this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,

System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.label6.Location = new System.Drawing.Point(168, 88);

this.label6.Name = "label6";

this.label6.Size = new System.Drawing.Size(43, 25);

this.label6.TabIndex = 20;

this.label6.Text = "张";

this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

//

// MyLotteryForm

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.BackColor = System.Drawing.Color.Bisque;

this.ClientSize = new System.Drawing.Size(458, 339);

this.Controls.Add(this.label6);

this.Controls.Add(this.pictureBox4);

this.Controls.Add(this.pictureBox3);

this.Controls.Add(this.pictureBox1);

this.Controls.Add(this.labelSort);

this.Controls.Add(this.pbSort);

this.Controls.Add(this.label5);

this.Controls.Add(this.pbEnd);

this.Controls.Add(this.label4);

this.Controls.Add(this.label3);

this.Controls.Add(this.labelDaytime);

this.Controls.Add(this.label2);

this.Controls.Add(this.label1);

this.Controls.Add(this.pbNextDraw);

this.Controls.Add(this.textBox6);

this.Controls.Add(this.textBox5);

this.Controls.Add(this.textBox4);

this.Controls.Add(this.textBox3);

this.Controls.Add(this.textBox2);

this.Controls.Add(this.textBox1);

this.ForeColor = System.Drawing.Color.IndianRed;

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;

this.MaximizeBox = false;

this.Name = "MyLotteryForm";

this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

this.Text = ".net彩票狐狸";

this.Load += new System.EventHandler(this.MyLotteryForm_Load);

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new MyLotteryForm());

}

private void MyLotteryForm_Load(object sender, System.EventArgs e)

{

//开始

int c=1000;

int[] numb = new int[c];

DateTime dt = new DateTime();

dt = DateTime.Now;

Random rnd = new Random(dt.Millisecond);

for (int i=0; i<c; i++)

{

numb[ i ] = rnd.Next(1,45);

}

Array.Sort(numb);

int k=0;

int j=0;

for (int i=0; i<c-1; i++)

{

if (numb[ i ] == numb[i+1])

{

result[k] = numb[ i ];

lottonumbers[k] = j+=1;

}

else

{

k+=1;

j=0;

}

}

Array.Sort(lottonumbers, result);

fnDisplay6Numbers(); // 第一个显示的数字

Array.Sort(result);

DateTime dt1= DateTime.Today; //取今天的日期啊,上帝保佑我中奖吧

this.labelDaytime.Text=dt1.ToLongDateString(); //显示日期

this.label4.Text=n_draw.ToString(); //显示数字

this.labelSort.Visible=false;

this.pbNextDraw.Focus();

//--------------结束

}

private void fnDisplay6Numbers()

{

// 在框里显示数字

this.textBox1.Text = result[49].ToString();

this.textBox2.Text = result[48].ToString();

this.textBox3.Text = result[47].ToString();

this.textBox4.Text = result[46].ToString();

this.textBox5.Text = result[45].ToString();

this.textBox6.Text = result[44].ToString();

}

private void fnDisplay6NumbersSorted()

{

int[] nr = new int[6];

nr[0]=Convert.ToInt16(this.textBox1.Text);

nr[1]=Convert.ToInt16(this.textBox2.Text);

nr[2]=Convert.ToInt16(this.textBox3.Text);

nr[3]=Convert.ToInt16(this.textBox4.Text);

nr[4]=Convert.ToInt16(this.textBox5.Text);

nr[5]=Convert.ToInt16(this.textBox6.Text);

Array.Sort(nr);

this.textBox1.Text = nr[0].ToString();

this.textBox2.Text = nr[1].ToString();

this.textBox3.Text = nr[2].ToString();

this.textBox4.Text = nr[3].ToString();

this.textBox5.Text = nr[4].ToString();

this.textBox6.Text = nr[5].ToString();

}

private void pbNextDraw_Click(object sender, System.EventArgs e)

{

this.MyLotteryForm_Load(sender, e);

n_draw++;

this.label4.Text=n_draw.ToString(); //在框里随便显示数字

//按“下注”的按钮后

this.pbSort.Enabled=true;

}

private void pbEnd_Click(object sender, System.EventArgs e)

{

Application.Exit();

}

private void pbSort_Click(object sender, System.EventArgs e)

{

fnDisplay6NumbersSorted(); //调用

this.pbSort.Enabled=false;

this.labelSort.Visible=true;

this.pbNextDraw.Focus();

}

private void labelDaytime_Click(object sender, System.EventArgs e)

{

}

private void label3_Click(object sender, System.EventArgs e)

{

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有