C#定时器和随机数

 更新时间:2015年06月17日 11:08:57   投稿:hebedich  
在前一篇中我们介绍了键盘和鼠标事件,其实还有一个非常常用的事件,就是定时器事件,如果要对程序实现时间上的控制,那么就要使用到定时器。而随机数也是很常用的一个功能,在我们要想产生一个随机的结果时就要使用到随机数。本文我们就来简单介绍一下定时器和随机数。

.net.Frameword中提供了一个专门产生随机数的类System.Random,此类默认情况下已被导入,编程过程中可以直接使用。我们知道,计算机并不能产生完全随机的数字,它生成的数字被称为伪随机数,它是以相同的概率从一组有限的数字中选取的,所选的数字并不具有完全的随机性,但就实用而言,其随机程度已经足够了。

我们来看下面的例子

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;
       }
    }
    //整数
    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
      RadioButton r = sender as RadioButton;
      if (r.Checked == true)
      {
        zheng = 1;
      }
    }
    //实数
    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;
    }
  }
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关文章

  • c#数据绑定之数据转化为信息的示例

    c#数据绑定之数据转化为信息的示例

    这篇文章主要介绍了c#数据绑定中的数据转化为信息的示例,需要的朋友可以参考下
    2014-04-04
  • C#中字段、属性、只读、构造函数赋值、反射赋值的问题

    C#中字段、属性、只读、构造函数赋值、反射赋值的问题

    这篇文章主要介绍了C#中字段、属性、只读、构造函数赋值、反射赋值的问题 ,非常不错,具有一定的参考借鉴借鉴价值,需要的朋友可以参考下
    2018-08-08
  • C#采用FileSystemWatcher实现监视磁盘文件变更的方法

    C#采用FileSystemWatcher实现监视磁盘文件变更的方法

    这篇文章主要介绍了C#采用FileSystemWatcher实现监视磁盘文件变更的方法,详细分析了FileSystemWatcher的用法,并以此为基础实现监视磁盘文件变更,是非常实用的技巧,具有一定的借鉴价值,需要的朋友可以参考下
    2014-11-11
  • C#创建自定义控件的示例

    C#创建自定义控件的示例

    这篇文章主要介绍了C#创建自定义控件的示例,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下
    2020-10-10
  • C#微信公众号开发之自定义菜单

    C#微信公众号开发之自定义菜单

    这篇文章介绍了C#微信公众号开发之自定义菜单,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • 解答“60k”大佬的19道C#面试题(上)

    解答“60k”大佬的19道C#面试题(上)

    这篇文章主要解答了“60k”大佬的19道C#面试题中的10道,文中的面试题比较小众,作者给了不错的答案,相信对你以后的面试有所帮助,感兴趣就来了解下
    2020-06-06
  • C#利用SharpPcap实现网络包捕获嗅探

    C#利用SharpPcap实现网络包捕获嗅探

    这篇文章主要为大家详细介绍了C#利用SharpPcap实现网络包捕获嗅探,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • C# 守护进程的介绍及实现详解

    C# 守护进程的介绍及实现详解

    本文主要介绍了C# 守护进程的介绍及实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • C#中如何生成安装包

    C#中如何生成安装包

    这篇文章主要介绍了C#中如何生成安装包问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • C#小程序15位转18位身份证号代码

    C#小程序15位转18位身份证号代码

    现在我们使用的都是18位身份证号,而以前都是15位身份证号,而如何将15位身份证号转18位身份证号转换为18位身份证号呢?
    2013-02-02

最新评论