C#定時器和隨機數(shù)
.net.Frameword中提供了一個專門產(chǎn)生隨機數(shù)的類System.Random,此類默認情況下已被導(dǎo)入,編程過程中可以直接使用。我們知道,計算機并不能產(chǎn)生完全隨機的數(shù)字,它生成的數(shù)字被稱為偽隨機數(shù),它是以相同的概率從一組有限的數(shù)字中選取的,所選的數(shù)字并不具有完全的隨機性,但就實用而言,其隨機程度已經(jīng)足夠了。
我們來看下面的例子
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ù)
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
RadioButton r = sender as RadioButton;
if (r.Checked == true)
{
shi = 2;
}
}
//開始
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;
}
//很不錯的算法
public static double GetDoubleRandomNumber(int min,int max)
{
Random r = new Random();
//很不錯的算法
double m=r.NextDouble() * max;
double n = r.NextDouble() * min;
if(m-n>2.0)
return m;
else
return n+3.0;
}
}
}
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
C# 利用IRawPixels接口遍歷柵格數(shù)據(jù)
本文主要介紹了利用IRawPixels接口遍歷柵格數(shù)據(jù)。具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02
C# 委托的三種調(diào)用示例(同步調(diào)用 異步調(diào)用 異步回調(diào))
本文將主要通過同步調(diào)用、異步調(diào)用、異步回調(diào)三個示例來講解在用委托執(zhí)行同一個加法類的時候的的區(qū)別和利弊2013-12-12
基于C#實現(xiàn)Windows服務(wù)的方法詳解
在實際應(yīng)用過程中,有時候我們希望開發(fā)的程序,不需要界面,直接開機就可以長時間運行,這時候,我們可以考慮做成一個Windows服務(wù)。這篇文章跟大家介紹一下,如何基于C#實現(xiàn)Windows服務(wù)的創(chuàng)建、安裝、啟動、停止和卸載,需要的可以參考一下2022-09-09
C# TreeView從數(shù)據(jù)庫綁定數(shù)據(jù)的示例
這篇文章主要介紹了C# TreeView從數(shù)據(jù)庫綁定數(shù)據(jù)的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
C#跨平臺開發(fā)之使用C/C++生成的動態(tài)鏈接庫
這篇文章介紹了C#跨平臺開發(fā)之使用C/C++生成的動態(tài)鏈接庫,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01

