Trouble Timer random interval

moonlord

Member
Joined
May 7, 2008
Messages
15
Programming Experience
1-3
Hi all,

I am a visual studio begginer and I have some problems in programming a simple app. What I want to do is to show the current step into a label at a random time interval.

Example. Total steps - value specified in textbox1 is 10

Step 1....after 10sec.....label shows Message 1
Step 2....after 15sec.....label shows Message 2
Step 3....after 5sec.......label shows Message 3
.....................................................................
Step 10...after 8sec.......label shows Message 10

Below is the code I have managed to write so far. Can anyone please help me with this. I would like to thank you in advance. I would really appreciate your help.
VB.NET:
Private Sub B2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B2.Click
        counter = 1
        Randomize()
        Timer1.Enabled = True
        Timer1.Interval = Int(10000 * Rnd())
    End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim a As Integer
        a = Val(TextBox1.Text)
        If counter >= a Then
            Timer1.Enabled = False
            counter = 1
        Else
            Label1.Text = "Message: " & counter.ToString
            counter = counter + 1
        End If
End Sub
 
Last edited by a moderator:
Each tick that occurs, you want the Interval of time passing to be random.. so why do you only set the Interval once, rather than in each tick?
 
System.Data.cjard that is correct "Each tick that occurs, you want the Interval of time passing to be random"

Can you please be more specific about setting the Interval once, rather than in each tick and obtaining for each step a random interval.

I would really appreciate it.

Thanks
 
System.Data.cjard that is correct "Each tick that occurs, you want the Interval of time passing to be random"

Can you please be more specific about setting the Interval once, rather than in each tick and obtaining for each step a random interval.

I would really appreciate it.

Thanks

What he means is that in the timer's tick event, you never change the timer's interval property. What you should do is have your random object grab a new random number and stick it in the interval property
 
VB.NET:
Private RandomInterval As New Random
Private a, Counter As Integer
Private HighestValue As Integer = 1 'Put your highest value here

Private Sub B2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B2.Click
    counter = 1
    a = CInt(TextBox1.Text)
    Timer1.Interval = 10001I + RandomInterval.Next(0, HighestValue + 1)
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If counter >= a Then
        Timer1.Enabled = False
        counter = 1
    Else
        Timer1.Interval = 10001I + RandomInterval.Next(0, HighestValue + 1)
        Label1.Text = "Message: " & counter.ToString
        Counter += 1
    End If
End Sub
 
Basically I am a beginner and I want to learn. I have just started with Visual Studio and I really like it. The best way to learn is to learn by examples, examples provided by experts like you with a lot of experience in the field. Your help is really appreciated. You were some time ago beginners like me and you know how hard was to put together a simple application. I just want to learn and sorry if I troubled you with my simple code. I would like to thank you kindly for your help

P.S. I really like VS and I am here to stay because this forum is great, full of great people always willing to teach you something useful. Thanks again
 
Basically I am a beginner and I want to learn. I have just started with Visual Studio and I really like it. The best way to learn is to learn by examples, examples provided by experts like you with a lot of experience in the field. Your help is really appreciated. You were some time ago beginners like me and you know how hard was to put together a simple application. I just want to learn and sorry if I troubled you with my simple code. I would like to thank you kindly for your help

P.S. I really like VS and I am here to stay because this forum is great, full of great people always willing to teach you something useful. Thanks again

As long as you try the task yourself first, asking here for help wont be a problem.

So basically, you wanted someone to do your homework for you?

Basically, yes.
 
I have tried your code. It seems the time interval is not random, it generates a message at every 10 seconds, every time Tried to tweak the code, no results, any clue?
 
Are you changing the 'HighestValue' integer at the top? I set it to one in my example because I don't know what the highest value could be
 
Yes I have changed it to 10. I even tried with 1000 and the same thing. The interval is set for each tick at 10 sec. It seems that the ticks are not random, any other idea? Thanks
 
I couldn't tell you, the code I posted has the interval being changed in the tick event.

It would help if you posted the code that you have now, I may be able to figure out why it's not working correctly for ya.
 
the code is the one you posted before, character by character, i have just added on the form a label, a button and a textbox. that's all.
 
Back
Top