SoFunction
Updated on 2025-03-07

C# timer and random number

. provides a class that specializes in producing random numbers. This class has been imported by default and can be used directly during programming. We know that a computer cannot generate completely random numbers. The number it generates is called a pseudo-random number. It is selected from a limited set of numbers with the same probability. The selected number is not completely random, but practically, its randomness is sufficient.

Let's look at the following example


using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
//using ;
namespace example3
{
  public partial class MainForm : Form
  {
    Timer timer = new Timer();
    int zheng;
    int shi;
     
    public MainForm()
    {
      InitializeComponent();
      +=button1_Click;
     +=button2_Click;
      
      // if ( != null)
      // {
       //  string m = ;
       
    }
 
    void timer_Tick(object sender, EventArgs e)
    {
      //throw new NotImplementedException();
    //  radioButton2_Click(null,null);
     //  double r = (((), ()));
    //  string s = ();
    //    = s;
      if (zheng == 1)
      {
        int r = (((), ()));
        string s = ();
         = s;
      }
       if (shi == 2)
      {
        double r = (((), ()));
          string s = ();
           = s;
       }
    }
    //Integer    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
      RadioButton r = sender as RadioButton;
      if ( == true)
      {
        zheng = 1;
      }
    }
    //Real number    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
      RadioButton r = sender as RadioButton;
      if ( == true)
      {
        shi = 2;
      }
    }
    //start    private void button1_Click(object sender, EventArgs e)
    {
       = ();
      // = 500;
       += timer_Tick; 
      ();
       
    }
    //stop    private void button2_Click(object sender, EventArgs e)
    {
      ();
    }
   
  }
}

using System;
using ;
using ;
using ;
using ;
//using ;
 
namespace example3
{
  class RandomHelp
  {
    public static int GetIntRandomNumber(int min,int max)
    {
      Random r=new Random();
      int ran=(min, max + 1);
 
    return ran;
    }
    // Very good algorithm    public static double GetDoubleRandomNumber(int min,int max)
    {
      Random r = new Random();
 // Very good algorithm      double m=() * max;
      double n = () * min;
      
      if(m-n>2.0)
      return m;
      else
      return n+3.0;
    }
  }
}

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