• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#定时器和随机数

C#定时器和随机数

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了马桶c的个人空间,c语言,欲情 c max,维生素c,crh2c等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

</div>

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;
    }
  }
}
</div>

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

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#利用ReportViewer生成报表
  • C#基于正则去掉注释的方法示例
  • C#中new的用法及与override的区别分析
  • C#实现两个richtextbox控件滚动条同步滚动的简单方法
  • C# for循环的经典案例集锦
  • C#操作word的方法示例
  • C#使用WebClient登录网站并抓取登录后的网页信息实现方法
  • C# WinForm制作异形窗体与控件的方法
  • C#实现Excel表数据导入Sql Server数据库中的方法
  • C#使用NPOI上传excel

相关文章

  • 2017-05-28c#学习之30分钟学会XAML
  • 2017-05-28c#访问this关键字和base关键字示例
  • 2017-05-28详解StackExchange.Redis通用封装类分享
  • 2017-05-28C#中数组初始化与数组元素复制的方法
  • 2017-05-28深入理解C#索引器(一种支持参数的属性)与属性的对比
  • 2017-05-28C# MeasureString测量字符串函数的使用方法
  • 2017-05-28C#文件目录操作方法汇总
  • 2017-05-28C#利用win32 Api 修改本地系统时间、获取硬盘序列号
  • 2017-05-28C#使用Parallel类进行多线程编程实例
  • 2017-05-28C#使用semaphore来管理异步下载请求的方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#开发中的垃圾回收机制简析
    • WPF换肤设计原理浅析
    • C#判断系统是32位还是64位的方法
    • C# JsonHelper 操作辅助类,拿来直接用
    • C#微信公众号开发 微信事件交互
    • C#多线程学习之(四)使用线程池进行多线程的自动管理
    • 使用XmlSerializer序列化List对象成XML格式(list对象序列化)
    • c#访问this关键字和base关键字示例
    • C#删除文件夹和文件到回收站示例
    • c# 调用Surfer软件,添加引用的具体操作方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有