C#定時(shí)器和隨機(jī)數(shù)
.net.Frameword中提供了一個(gè)專(zhuān)門(mén)產(chǎn)生隨機(jī)數(shù)的類(lèi)System.Random,此類(lèi)默認(rèn)情況下已被導(dǎo)入,編程過(guò)程中可以直接使用。我們知道,計(jì)算機(jī)并不能產(chǎn)生完全隨機(jī)的數(shù)字,它生成的數(shù)字被稱(chēng)為偽隨機(jī)數(shù),它是以相同的概率從一組有限的數(shù)字中選取的,所選的數(shù)字并不具有完全的隨機(jī)性,但就實(shí)用而言,其隨機(jī)程度已經(jīng)足夠了。
我們來(lái)看下面的例子
MainForm.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; //using example3.RandomHelp; namespace example3 { public partial class MainForm : Form { Timer timer = new Timer(); int zheng; int shi; public MainForm() { InitializeComponent(); button1.Click+=button1_Click; button2.Click+=button2_Click; // if (textBox3.Text != null) // { // string m = textBox3.Text; } void timer_Tick(object sender, EventArgs e) { //throw new NotImplementedException(); // radioButton2_Click(null,null); // double r = (example3.RandomHelp.GetIntRandomNumber(int.Parse(textBox1.Text), int.Parse(textBox2.Text))); // string s = r.ToString(); // label4.Text = s; if (zheng == 1) { int r = (example3.RandomHelp.GetIntRandomNumber(int.Parse(textBox1.Text), int.Parse(textBox2.Text))); string s = r.ToString(); label4.Text = s; } if (shi == 2) { double r = (example3.RandomHelp.GetDoubleRandomNumber(int.Parse(textBox1.Text), int.Parse(textBox2.Text))); string s = r.ToString(); label4.Text = s; } } //整數(shù) private void radioButton1_CheckedChanged(object sender, EventArgs e) { RadioButton r = sender as RadioButton; if (r.Checked == true) { zheng = 1; } } //實(shí)數(shù) private void radioButton2_CheckedChanged(object sender, EventArgs e) { RadioButton r = sender as RadioButton; if (r.Checked == true) { shi = 2; } } //開(kāi)始 private void button1_Click(object sender, EventArgs e) { timer.Interval = int.Parse(textBox3.Text); //timer.Interval = 500; timer.Tick += timer_Tick; timer.Start(); } //停止 private void button2_Click(object sender, EventArgs e) { timer.Stop(); } } }
RandomHelp.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //using System.Windows.Forms.Timer; namespace example3 { class RandomHelp { public static int GetIntRandomNumber(int min,int max) { Random r=new Random(); int ran=r.Next(min, max + 1); return ran; } //很不錯(cuò)的算法 public static double GetDoubleRandomNumber(int min,int max) { Random r = new Random(); //很不錯(cuò)的算法 double m=r.NextDouble() * max; double n = r.NextDouble() * min; if(m-n>2.0) return m; else return n+3.0; } } }
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
c#數(shù)據(jù)綁定之?dāng)?shù)據(jù)轉(zhuǎn)化為信息的示例
這篇文章主要介紹了c#數(shù)據(jù)綁定中的數(shù)據(jù)轉(zhuǎn)化為信息的示例,需要的朋友可以參考下2014-04-04C#中字段、屬性、只讀、構(gòu)造函數(shù)賦值、反射賦值的問(wèn)題
這篇文章主要介紹了C#中字段、屬性、只讀、構(gòu)造函數(shù)賦值、反射賦值的問(wèn)題 ,非常不錯(cuò),具有一定的參考借鑒借鑒價(jià)值,需要的朋友可以參考下2018-08-08C#采用FileSystemWatcher實(shí)現(xiàn)監(jiān)視磁盤(pán)文件變更的方法
這篇文章主要介紹了C#采用FileSystemWatcher實(shí)現(xiàn)監(jiān)視磁盤(pán)文件變更的方法,詳細(xì)分析了FileSystemWatcher的用法,并以此為基礎(chǔ)實(shí)現(xiàn)監(jiān)視磁盤(pán)文件變更,是非常實(shí)用的技巧,具有一定的借鑒價(jià)值,需要的朋友可以參考下2014-11-11C#利用SharpPcap實(shí)現(xiàn)網(wǎng)絡(luò)包捕獲嗅探
這篇文章主要為大家詳細(xì)介紹了C#利用SharpPcap實(shí)現(xiàn)網(wǎng)絡(luò)包捕獲嗅探,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03C#?守護(hù)進(jìn)程的介紹及實(shí)現(xiàn)詳解
本文主要介紹了C#?守護(hù)進(jìn)程的介紹及實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06C#小程序15位轉(zhuǎn)18位身份證號(hào)代碼
現(xiàn)在我們使用的都是18位身份證號(hào),而以前都是15位身份證號(hào),而如何將15位身份證號(hào)轉(zhuǎn)18位身份證號(hào)轉(zhuǎn)換為18位身份證號(hào)呢?2013-02-02