1

我正在尝试使用下面的代码以随机间隔运行计时器。问题是,当我这样做时,我只发布了 1 个随机数,而且我不确定如何使用我的代码获取下一个随机数:

using System;
using System.Windows.Forms;

namespace Auto_Typer
{
    public partial class AutoTyper : Form
    {
        public AutoTyper()
        {
            InitializeComponent();

            tmrInterval.Tick += new EventHandler(Interval);

            txtInterval.TextChanged += new EventHandler(TextChanged);
            txtMin.TextChanged += new EventHandler(TextChanged);
            txtMax.TextChanged += new EventHandler(TextChanged);
        }

        private void TextChanged(object sender, EventArgs e)
        {
            if (int.Parse(txtInterval.Text) < 1 || int.Parse(txtMin.Text) < 1 || int.Parse(txtMax.Text) < 1)
            {
                txtInterval.Text = "1";
                txtMin.Text = "1";
                txtMax.Text = "1";
            }
            else if (int.Parse(txtInterval.Text) > 100)
            {
                txtInterval.Text = "100";
                txtMin.Text = "100";
                txtMax.Text = "100";
            }
            else if (int.Parse(txtMin.Text) >= int.Parse(txtMax.Text))
            {
                txtMax.Text = (int.Parse(txtMin.Text) + 1).ToString();
            }
        }

        void Interval(object sender, EventArgs e)
        {
            SendKeys.Send(txtText.Text + ", " + tmrInterval.Interval + "{enter}");
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (tbType.SelectedTab == tbInterval)
            {
                tmrInterval.Interval = int.Parse(txtInterval.Text) * 1000;
                tmrInterval.Enabled = true;
            }

            if (tbType.SelectedTab == tbRange)
            {
                Random random = new Random();

                tmrInterval.Interval = (random.Next(int.Parse(txtMin.Text), int.Parse(txtMax.Text)) * 1000);
                tmrInterval.Enabled = true;
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            tmrInterval.Enabled = false;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
4

3 回答 3

4

我建议将“new Random()”调用移到您的初始化程序中,然后在每次需要新的随机数时简单地调用 Random.Next()。

有关 Random() 的更多信息,请访问:http: //msdn.microsoft.com/en-us/library/h343ddh9.aspx

随机不是真正的随机,它是伪随机。这意味着给定相同的起始种子(给 Random 实例化的参数),对 Random.Next() 的后续调用每次都将返回相同的值序列。好消息(感谢 phoog)是默认构造函数(无参数)自动使用基于时间的种子。

于 2012-01-06T02:58:22.880 回答
2

只需将回调方法中的间隔更改为随机更改的间隔:

private Random random = new Random();

void Interval(object sender, EventArgs e)
{
    tmrInterval.Interval = (random.Next(int.Parse(txtMin.Text), int.Parse(txtMax.Text)) * 1000);
    SendKeys.Send(txtText.Text + ", " + tmrInterval.Interval + "{enter}");
}

并且(如上)使Random实例成为类的成员字段,这样您就不必每次都重新更新它(如果以非常快的顺序调用可能会带来问题)。

于 2012-01-06T03:00:04.390 回答
1

Right now the Random instance is created every time the button is clicked because it's a local of the handler. To persist it between presses you need to make it a field. -

private Random _random = new Random();

Now the click handler can get the next random value from the field

if (tbType.SelectedTab == tbRange)
{
    tmrInterval.Interval = (_random.Next(int.Parse(txtMin.Text), int.Parse(txtMax.Text)) * 1000);
    tmrInterval.Enabled = true;
}
于 2012-01-06T03:07:12.953 回答