本文實例講述了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();//關閉程序 } }}PS:這里再為大家提供幾款功能類似的在線工具供大家參考:
在線隨機數字/字符串生成工具:
http://tools.VeVB.COm/aideddesign/suijishu
在線隨機字符/隨機密碼生成工具:
http://tools.VeVB.COm/aideddesign/rnd_password
高強度密碼生成器:
http://tools.VeVB.COm/password/CreateStrongPassword
更多關于C#相關內容感興趣的讀者可查看本站專題:《C#數據結構與算法教程》、《C#程序設計之線程使用技巧總結》、《C#常見控件用法教程》、《WinForm控件用法總結》、《C#數組操作技巧總結》及《C#面向對象程序設計入門教程》
希望本文所述對大家C#程序設計有所幫助。
新聞熱點
疑難解答