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;
}
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。
0
投稿
猜你喜欢
- 本文实例讲述了C#中数组初始化、反转和排序用法。分享给大家供大家参考。具体如下:下面的代码演示了在C#中定义和初始化数组,然后对其进行赋值,
- 本文实例为大家分享了Java实现简单猜拳游戏的具体代码,供大家参考,具体内容如下看网上的猜拳游戏那么多,但都是用switch输入数字,所以用
- 归并排序简单解释:该算法是采用分治法,把数组不断分割,直至成为单个元素,然后比较再合并(合并的过程就是两部分分别从头开始比较,取出最小或最大
- SpringBoot底层的一个功能 : @ConfigurationProperties@ConfigurationProperties 配
- 做快递面单打印模板,快递要求纸张大小100 x 150mm。PageSize.A4=595 x 842A4尺寸=210mm×297mm故设置
- Interface Segregation Principle,ISP接口隔离原则主张使用多个专门的接口比使用单一的总接口要好。一个类对另外
- 在项目中遇到try...catch...语句,因为对Java异常处理机制的流程不是很清楚,导致对相关逻辑代码不理解。所以现在来总结Java异
- 前言 spring事务管理包含两种情况,编程式事务、声明
- 一个图片识别小工具,原先主要是识别以前公司的软件注册码截图里的数字和字母(每次要一个一个框复制出来粘贴到注册器里,很麻烦!),因为注册码出现
- 在项目中用到了MapStruct,对其可以转换JavaBean特别好奇,因为之前都是使用Vo的方式手动set转换,但是接触到MapStruc
- 在后台工程师开发完新代码交给QA进行测试时,软件测试人员一般都会要求后台开发对单元测试的覆盖率达到一定的标准;例如我们的标准是分支覆盖率达到
- 可以使用 graphics2D 类提供的 shear(
- 接触过Android开发的同学们都知道在Android中访问程序资源基本都是通过资源ID来访问。这样开发起来很简单,并且可以不去考虑各种分辨
- 出错信息:Unknown error (0xffffffff)at System.Diagnostics.Process.StartWith
- 为什么要在J2EE项目中谈异常处理呢?可能许多java初学者都想说:“异常处理不就是try….catch…finally吗?这谁都会啊!”。
- 准备:wildfly/tomcat或者其他服务器你的数据库的Driver,(此处用的mysql-connecter-java-5.1.39-
- 一、整合原理activiti的配置文件本身就是一个spring的配置文件,但默认情况下只讲ProcessEngineConfiguratio
- 本文实例讲述了Java包装类原理与用法。分享给大家供大家参考,具体如下:产生:为了提高数据类型的的面向对象性,所以产生了包装类,包装类中有各
- 温馨提示:本教程的 GitHub 地址为「intellij-idea-tutorial」,欢迎感兴趣的童鞋Star、Fork,纠错。首先,给
- 本文实例总结了C#生成随机数的方法。分享给大家供大家参考。具体分析如下:开始,很简单地使用System.Random类来生成随机数。很快,问