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)
{
}
}
}