当前位置: 移动技术网 > IT编程>开发语言>c# > C#定时器和随机数

C#定时器和随机数

2019年07月18日  | 移动技术网IT编程  | 我要评论
.net.frameword中提供了一个专门产生随机数的类system.random,此类默认情况下已被导入,编程过程中可以直接使用。我们知道,计算机并不能产生完全随机的数

.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;
    }
  }
}

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

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网