C Timer and Random Number

  • 2021-06-29 11:49:35
  • OfStack

In.net.Frameword, a class, System.Random, is provided that specifically generates random numbers, which are imported by default and can be used directly in programming.We know that computers do not produce completely random numbers. The numbers they produce are called pseudo-random numbers. They are selected from a limited set of numbers with the same probability. The selected numbers are not completely random, but they are practical enough.

Let's look at the following examples

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;
       }
    }
    // integer 
    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
      RadioButton r = sender as RadioButton;
      if (r.Checked == true)
      {
        zheng = 1;
      }
    }
    // real number 
    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
      RadioButton r = sender as RadioButton;
      if (r.Checked == true)
      {
        shi = 2;
      }
    }
    // start 
    private void button1_Click(object sender, EventArgs e)
    {
      timer.Interval = int.Parse(textBox3.Text);
      //timer.Interval = 500;
      timer.Tick += timer_Tick; 
      timer.Start();
       
    }
    // Stop it 
    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;
    }
    // Good algorithm 
    public static double GetDoubleRandomNumber(int min,int max)
    {
      Random r = new Random();
 // Good algorithm     
      double m=r.NextDouble() * max;
      double n = r.NextDouble() * min;
      
      if(m-n>2.0)
      return m;
      else
      return n+3.0;
    }
  }
}

The above is the whole content of this article, I hope you like it.


Related articles: