c#定时器和随机数

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
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
这是老师做过的一个项目,抽取了当中核心之一的内容给大家试试,有些难度。 1、实验目的 1)掌握C#命令行参数的接收; 2)掌握C#中的泛型用法; 3)掌握C#的基本流程语句; 4)掌握C#随机数生成; 5)了解C#的异常处理流程; 6)掌握C#的计时器及对代码的性能优化。 7)本实验注重算法实现,在实现功能的前提下,尽量优化计算速度。 2、实验要求 这是一个实际的项目衍生出来的核心算法之一。防伪码是我们现在经常在商品上看到的防伪手段之一,现在需要编写一个防伪码生成器,按照输入参数生成防伪码,并且把生成的时间及指定的防伪码输出。 1)防伪码的组成 防伪码由以下字符组成:0123456789ABCDEFGHJKLMNPQRSTUVWXYZ (数字1和字母I相近、数字0和字母O相近,所以去掉字母I和字母O。全部字母大写) \ 2)在命令行中输入2个参数,分别是: 防伪码长度 防伪码个数 例如:在命令行中调用程序为:学号.exe 10 10000 指的是防伪码长度为10,生成10000个防伪码。 3)防伪码的生成及注意事项 防伪码的长度由命令行参数决定; 所生成的防伪码不能重复(按照以上例子,生成了10000个防伪码,这10000个防伪码就肯定不能重复)。 3、提交内容 1)请把整个项目源代码压缩为RAR文件进行提交 2)运行: a) exe 10 10000 b) exe 20 1000000 c) exe 50 1000000 (即防伪码长度为10,10000个;长度为20,一百万个;长度为50,一百万个)共三个测试用例,记下时间,贴在“自评备注”中,格式如下: a) 时间;b) 时间;c) 时间 下面可以写写自己的感想等 4、实验评分 程序能运行,按照全部要求实现:A及以上 程序能运行,但有Bug的(指生成重复的防伪码等):B 程序不能运行,但有思路的:C及以下 0:发现抄袭行为,本次实验0分,累计两次,平时成绩0分。 额外要求:代码命名必须有一定规范、代码格式必须靓仔,歪歪扭扭的扣分。 5、采用的函数及思路 1)开发时,在解决方案中,鼠标右键选择项目,然后点选“属性”,在“调试”项目中的“命令行参数”里面,可以预先输入需在命令行接收的参数,这样开发的时候就可以直接读入了。注意,不用对这些参数进行校验,默认输入的都是正确的参数,注意程序运行时的异常处理。 2)程序运行计时 在最上面加入using System.Diagnostics; 在程序头加入: Stopwatch timer1 = new Stopwatch();//计时器类 timer1.Start();//开始计时 在程序最尾加入: timer1.Stop();//停止计时 double dMilliseconds = timer1.Elapsed.TotalMilliseconds; Console.WriteLine("生成个数为:{0},运行时间为:{1}", icount, dMillisecondes); Console.ReadKey(); Stopwatch是C#一个类似秒表的东西,用来计算程序的运行时间,注意,必须按照要求,计时器在程序运行时就要开启,在结束时停止并输出结果,不能放置在其他地方。 3)防伪码生成思路 例如,有以下定义: string strTableChar = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"; 生成一个从0到strTableChar.Length的数字a,然后使用strTableChar[a]就可以随机返回一个字母,重复n次(n等于防伪码的长度),这样就可以组合到一串随机字符串,也就是防伪码了。 6、思路及技巧 1)随机数生成:种子的选择问题,可用默认的、GUID、RNGCryptoServiceProvider等等作为随机数种子…… 2)怎么样保证,新生成的防伪码和以生成的防伪码有没有重复? 3)stringBuilder的用法,试试用string和stringBuilder有什么不同?听听老师的课…… 4)多次运行程序后,会不会发现程序运行速度会加快?看看这里:《告诉一个不一样的.NET Framework字符串驻留》,另外也可以找找对应.NET Framework底层实现的文章。
要在C#中使用ZedGraph实时绘制数据曲线,你可以使用定时器或者后台线程来更新数据,并动态地将新数据添加到图表中。下面是一个简单的示例代码,展示了如何实现实时数据曲线的绘制: ```csharp using System; using System.Drawing; using System.Windows.Forms; using ZedGraph; namespace RealTimeGraph { public partial class Form1 : Form { private ZedGraphControl zedGraphControl1; private GraphPane graphPane; private RollingPointPairList dataPoints; private LineItem curve; private double xAxisValue = 0; private Timer timer; public Form1() { InitializeComponent(); // 创建ZedGraph控件 zedGraphControl1 = new ZedGraphControl(); zedGraphControl1.Dock = DockStyle.Fill; this.Controls.Add(zedGraphControl1); // 创建图表对象 graphPane = zedGraphControl1.GraphPane; // 设置图表的标题 graphPane.Title.Text = "实时数据曲线示例"; // 设置X轴和Y轴的标签 graphPane.XAxis.Title.Text = "时间"; graphPane.YAxis.Title.Text = "数据值"; // 创建数据点列表 dataPoints = new RollingPointPairList(1000); // 创建曲线对象 curve = graphPane.AddCurve("曲线名称", dataPoints, Color.Blue, SymbolType.None); // 创建定时器 timer = new Timer(); timer.Interval = 1000; // 每隔1秒更新一次数据 timer.Tick += Timer_Tick; } private void Form1_Load(object sender, EventArgs e) { // 启动定时器 timer.Start(); } private void Timer_Tick(object sender, EventArgs e) { // 更新数据 double newData = GetNewData(); // 添加新数据点 dataPoints.Add(xAxisValue, newData); xAxisValue += 1; // 刷新图表 zedGraphControl1.AxisChange(); zedGraphControl1.Invalidate(); } private double GetNewData() { // 这里可以根据你的需求获取新的数据值,这里仅作示例,使用随机数生成 Random random = new Random(); return random.Next(1, 10); } } } ``` 在这个示例中,我们创建了一个窗体,并将ZedGraph控件添加到窗体上。在窗体加载事件中启动定时器,并在定时器的Tick事件中更新数据并刷新图表。 每次定时器触发时,我们通过`GetNewData()`方法获取新的数据值,并将其添加到数据点列表中。然后,通过调用`AxisChange()`方法和`Invalidate()`方法来刷新图表。 你可以根据自己的需求调整定时器的间隔和数据获取的逻辑。希望这个示例对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

9号信箱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值