C#定时器和随机数

作者:hebedich 时间:2023-10-28 15:30:34 

.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#,定时器,随机数
0
投稿

猜你喜欢

  • unity AudioSource播放完声音后要执行的函数或条件操作

    2021-07-04 21:46:37
  • C语言折半查找法的超详细讲解

    2022-10-26 19:33:16
  • Android 通过onDraw实现在View中绘图操作的示例

    2023-07-14 02:15:38
  • Android中去掉标题栏的几种方法(三种)

    2023-04-01 10:57:37
  • IDEA 2020.2 +Gradle 6.6.1 + Spring Boot 2.3.4 创建多模块项目的超详细教程

    2021-11-08 00:42:36
  • 线程池中使用spring aop事务增强

    2021-08-06 06:37:19
  • android开发基础教程—SharedPreferences读写

    2022-11-30 08:07:16
  • android使用Path绘制出多边形

    2021-11-11 19:53:38
  • Struts2中Action中是否需要实现Execute方法

    2021-10-30 06:57:23
  • java的内部类和外部类用法讲解

    2022-10-18 21:14:41
  • java中JSONObject转换为HashMap(方法+main方法调用实例)

    2023-08-10 04:04:08
  • Java快速排序QuickSort(实例)

    2021-12-22 21:47:42
  • Unity 静态变量跨场景操作

    2023-10-16 14:32:41
  • Android SeekBar实现平滑滚动

    2022-01-21 10:51:24
  • android开发教程之判断是手机还是平板的方法

    2022-10-22 12:30:41
  • Go Java算法之K个重复字符最长子串详解

    2022-02-10 17:53:29
  • C# wpf 无边框窗口添加阴影效果的实现

    2023-11-05 01:15:09
  • Spring boot实现文件上传功能

    2023-08-01 07:00:02
  • Unity 按钮事件封装操作(EventTriggerListener)

    2022-07-08 10:07:08
  • Android应用中ListView利用OnScrollListener分页加载数据

    2021-11-06 16:01:04
  • asp之家 软件编程 m.aspxhome.com