本文實例講述了C#實現的簡單隨機數產生器功能。分享給大家供大家參考,具體如下:
運行效果如下:
具體代碼如下:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Math{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } System.Random t=new System .Random() ;//初始化隨機數函數 string num;//定義一個字符串變量,用于顯示 private void button1_Click(object sender, EventArgs e) { timer1.Start(); } private void button2_Click(object sender, EventArgs e) { timer1.Stop(); } private void timer1_Tick(object sender, EventArgs e) { this.num = Convert.ToString(t.Next(1,1000));//產生1到1000之間的一個隨機數 textBox1.Text = num; } private void button3_Click(object sender, EventArgs e) { this.Close();//關閉程序 } }}
希望本文所述對大家C#程序設計有所幫助。
新聞熱點
疑難解答