C#定时器和随机数

所属分类: 软件编程 / C#教程 阅读数: 75
收藏 0 赞 0 分享

.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#抽象类与抽象方法的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C#代码实现扑克牌排序的几种方式

今天小编就为大家分享一篇关于C#代码实现扑克牌排序,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#泛型概念的简介与泛型的使用

今天小编就为大家分享一篇关于C#泛型概念的简介与泛型的使用,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C# 7.0 使用下划线忽略使用的变量的原因分析

这篇文章主要介绍了C# 7.0 使用下划线忽略使用的变量的原因浅析,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C# 中使用正则表达式匹配字符的含义

正则表达式的作用用来描述字符串的特征。本文重点给大家介绍C# 中使用正则表达式匹配字符的含义,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

C# Dictionary和SortedDictionary的简介

今天小编就为大家分享一篇关于C# Dictionary和SortedDictionary的简介,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#中SQL Command的基本用法

今天小编就为大家分享一篇关于C#中SQL Command的基本用法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#使用SQL DataReader访问数据的优点和实例

今天小编就为大家分享一篇关于C#使用SQL DataReader访问数据的优点和实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#使用SQL Dataset数据集代码实例

今天小编就为大家分享一篇关于的文章,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#使用SQL DataAdapter数据适配代码实例

今天小编就为大家分享一篇关于C#使用SQL DataAdapter数据适配代码实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享
查看更多