HelpNeeder
HelpNeeder

Reputation: 6490

Use timer with random interval

I am trying to run the timer in random interval by using code below. The problem is that when I do this I am posted only 1 random number and I'm not sure how to fetch next random number with my code:

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();
        }
    }
}

Upvotes: 1

Views: 5413

Answers (3)

Fletch
Fletch

Reputation: 963

I would suggest moving the "new Random()" call out to your initializer, then simply calling Random.Next() each time you need a new random number.

more info on Random() at: http://msdn.microsoft.com/en-us/library/h343ddh9.aspx

Random isn't really random, it's pseudo-random. This means that given the same starting seed (the argument given to the Random instantiation), subsequent calls to Random.Next() will return the same sequence of values every time. The good news (thanks, phoog) is that the default constructor (no args) automatically uses a time-based seed.

Upvotes: 4

JaredPar
JaredPar

Reputation: 755141

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;
}

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160942

Just change the interval in the callback method for a randomly changing interval :

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}");
}

And also (like above) make the Random instance a member field of your class so you don't have to re-new it every time (which might bring problems if called in very fast order).

Upvotes: 2

Related Questions