C#定时器和随机数
作者:hebedich 发布时间:2023-10-28 15:30:34
标签:C#,定时器,随机数
.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;
}
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。


猜你喜欢
- PageAdapter是一个抽象类,直接继承于Object,导入包android.support.v4.view.PagerAdapter即
- 一、编码(转成字节数组)GetBytes 1、ASII码:字符串中的每个字符用一个字节表示。每个字符实际上只使用了7位,从00h
- 在本文中,我们将介绍二进制搜索相对于简单线性搜索的优势,并介绍它在 Java 中的实现。1. 需要有效的搜索假设我们在wine-sellin
- 上一篇中我们介绍了自定义实现BaseAdapter的普通实现布局,然而上一章也说了普通实现的方式效率会很低,而且对系统开销也很大,所以,那样
- 报错信息如下:java: 无法访问org.springframework.boot.SpringApplication错误的类文件: /C:
- 一个项目中需要使用两个数据库,Oracle 和Mysql,于是参考各个blog,实现此功能。写好后才发现,原来的事务失效了,我去...spr
- 本文实例为大家分享了java数字转汉字工具类的具体代码,供大家参考,具体内容如下/** * Created by 33303 on 2017
- 本文实例所述为Android天气预报之解析天气数据的代码,可实现获取HttpGet对象读取天气网站天气数据,并从数据中解析出天气数据,比如温
- 1.概述前几天群里有人问如何制作备忘录,感觉这样一个小实例挺适合新手们入门学习使用,所以就抽空做了出来。界面如下图这个备忘录主要包括了如下功
- java的比较器有两类,分别是Comparable接口和Comparator接口。在为对象数组进行排序时,比较器的作用非常明显,首先来讲解C
- 方法一、使用系统控件ViewFlipper方式:布局文件:<ViewFlipper andro
- webview要调起input-file拍照或者选取文件功能,可以在webview.setWebChromeClient方法中重写指定的方法
- ssm mybatis配置多个mapper目录通配符配置比如目录的结构如下:com/test/web/user/mappercom/test
- 具体配置方法如下: 1:在服务器上安装office的Excel软件. 2:在"开始"->"运行"
- 开发程序的时候经常会引用一些第三方的DLL,然后编译生成的exe文件就不能脱离这些DLL独立运行了。但是,很多时候我们本想开发一款只需要一个
- 一.解析概念StringUtils概念StringUtils 方法的操作对象是 Java.lang.String 类型的对象,是 JDK 提
- 介绍随着当今处理器中可用的核心数量的增加, 随着对实现更高吞吐量的需求的不断增长,多线程 API 变得非常流行。 Java 提供了自己的多线
- MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移
- 一、JMeter后端 * 介绍说到JMeter后端 * ,必须要从源头BackendListener开始说,最后延伸到我们需要的Backen
- 一、文件上传原理 1、文件上传的前提:a、form表单的method必须是postb、form表单的enctype必须是multi