Results 1 to 15 of 15

Thread: Recurring Random Countdown

  1. #1

    Thread Starter
    Lively Member Amerigo's Avatar
    Join Date
    Dec 2008
    Location
    PSR B1620-26 c-1
    Posts
    126

    Question Recurring Random Countdown

    Hello all,
    My program is supposed to have a recurring random countdown. That each time it loops, it should have a new random time to countdown from.
    Please look at my code and see what's wrong. I have tried several different ways, and nothing seems to work.
    Thank you in advance.
    Code:
    Public Class MainPanel
    
    
        Public Structure intimacyPlayer
            Dim Name As String
            Dim Age As Integer
            Dim Gender As String
            Dim attribute 1 As String
            Dim attribute 2 As String
            Dim attribute 3 As String
            Dim attribute 4 As String
            Dim attribute 5 As String
            Dim attribute 6 As String
            Dim attribute 7 As String
            Dim attribute 8 As String
        End Structure
    
        Public Structure intimacyPreference
            Dim KeepSettings As String
            Dim Player1 As String
            Dim Player2 As String
            Dim Player3 As String
            Dim Player4 As String
            Dim Player5 As String
            Dim Player6 As String
            Dim Player7 As String
            Dim Player8 As String
            Dim Player9 As String
            Dim Player10 As String
            Dim Player11 As String
            Dim Player12 As String
            Dim Player13 As String
            Dim Player14 As String
            Dim Level As String
            Dim Voice As String
            Dim Volume As Integer
            Dim Background As String
            Dim myFont As String
            Dim Objects As String
            Dim Things As String
            Dim Stuff As String
        End Structure
        Public myPlayers As New Collection
        Dim spkr As New SpeechLib.SpVoice
        Dim Min, Max As New Integer
        Dim r As New Random
        Dim CountDownTo As Integer = r.Next(10, 20)
        Dim ts As New TimeSpan(0, 0, (CountDownTo))
        Dim CountDown As New Stopwatch
        Dim split As String()
        Dim I As New Integer()
        Dim pfl As New Integer()
    
        Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '***MAKE SURE WE HAVE INFO TO USE***
    
            If System.IO.File.Exists(Application.StartupPath & "\Players.sex") = False Or System.IO.File.Exists(Application.StartupPath & "\preferences.sex") = False Then
    
                Dim myControlPanel As New ControlPanel
                myControlPanel.ShowDialog()
            End If
    
            '***CHECK IF 'KEEPSETTINGS' IS CHECKED*** (The users may want different options everytime.)
            Dim strinput As String = ""
            FileOpen(1, Application.StartupPath & "\preferences.sex", OpenMode.Input)
            Do Until strinput.StartsWith("Keep")
                Input(1, strinput)
                If strinput = "KeepSettings = 0" Then
                    Reset()
                    Dim myControlPanel As New ControlPanel
                    myControlPanel.ShowDialog()
                End If
            Loop
            FileClose(1)
    
            '***SHOW SPLASHSCREEN2****
    
            '***GET MIN AND MAX***
    
    
    
            Timer1.Interval = 100
            Timer1.Start()
    
    
    
    
    
    
            CountDown.Reset() : CountDown.Start()
    
        End Sub
    
    
        Private Sub controlpanelbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles controlpanelbutton.Click
    
            Reset()
            CountDown.Stop()
            Dim myControlPanel As New ControlPanel
            Dim result As DialogResult 'so we know what they chose
    
            result = myControlPanel.ShowDialog()
    
            If result = Windows.Forms.DialogResult.OK Then
            Else
                End
            End If
            CountDown.Start()
        End Sub
    
        Private Sub PauseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PauseButton.Click
            CountDown.Stop()
            MsgBox("Click Ok to continue", MsgBoxStyle.ApplicationModal, "PAUSED")
            CountDown.Start()
        End Sub
    
    
        Private Sub QuitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitButton.Click
            Me.Close()
            End
        End Sub
    
    
    
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
            Dim DurHold As String = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\preferences.sex")
    
            Dim DurDelimiters() As String = {vbNewLine}
            Dim DurTextLines() As String = DurHold.Split(DurDelimiters, StringSplitOptions.RemoveEmptyEntries)
    
            pfl = DurTextLines.Length
    
    
            For I = 1 To pfl
                If DurTextLines(I).StartsWith("Minimum") Then
                    split = DurTextLines(I).Split("=")
                    Min = Trim(split(1))
                    Exit For
                End If
            Next
            For I = 1 To pfl
                If DurTextLines(I).StartsWith("Maximum") Then
                    split = DurTextLines(I).Split("=")
                    Max = Trim(split(1))
                    Exit For
                End If
            Next
    
            'CountDownTo = r.Next(Min, Max)
    
    
            FileOpen(1, Application.StartupPath & "\test1.txt", OpenMode.Append)
            WriteLine(1, Min, Max, CountDownTo)
            Reset()
            If CountDown.IsRunning Then 'is the stopwatch running
                If CountDown.Elapsed.TotalSeconds >= CountDownTo Then 'have we reached zero
                    'yes, stop the stopwatch
                    CountDown.Stop()
    
                    '***RANDOMLY SELECT TWO PLAYERS***
                    Dim I, pfl As New Integer()
                    Dim split As String()
                    Dim Doer As String = ""
                    Dim Receiver As String = ""
                    Dim pick As Integer
                    Dim rand As New Random
                    Dim sHold As String = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\preferences.sex")
                    Dim profileHold As String = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Players.sex")
                    Dim sDelimiters() As String = {vbNewLine}
                    Dim sTextLines() As String = sHold.Split(sDelimiters, StringSplitOptions.RemoveEmptyEntries)
    
                    Dim profileDelimiters() As String = {vbNewLine}
                    Dim profileTextLines() As String = profileHold.Split(profileDelimiters, StringSplitOptions.RemoveEmptyEntries)
    
    RepickDoer:
                    '***PICK NAMES***
                    Do
                        pick = rand.Next(1, 15)
                        For I = 1 To pick
                            If sTextLines(pick).StartsWith("Play") Then
                                split = sTextLines(pick).Split("=")
                                Doer = Trim(split(1))
                            End If
                        Next
                    Loop While Doer = ""
                    Dim D_Name As String = ""
                    pfl = profileTextLines.Length
                    For I = 1 To pfl
                        If profileTextLines(I).StartsWith("Name") Then
                            split = profileTextLines(I).Split("=")
                            D_Name = Trim(split(1))
                            If D_Name = Doer Then
                                Exit For
                            End If
                        End If
                    Next
                    Dim D_Age As New Integer
                    split = profileTextLines(I + 1).Split("=")
                    D_Age = Trim(split(1))
                    If D_Age < 18 Then
                        GoTo RepickDoer
                    End If
    
    'Get Profile info
    
    RepickReceiver:
    
                    Do
                        pick = rand.Next(1, 15)
                        For I = 1 To pick
                            If sTextLines(pick).StartsWith("Play") Then
                                split = sTextLines(pick).Split("=")
                                Receiver = Trim(split(1))
                            End If
    
                        Next
                    Loop While Receiver = "" Or Receiver = Doer
    
                    Dim R_Name As String = ""
                    pfl = profileTextLines.Length
                    For I = 1 To pfl
                        If profileTextLines(I).StartsWith("Name") Then
                            split = profileTextLines(I).Split("=")
                            R_Name = Trim(split(1))
                            If R_Name = Receiver Then
                                Exit For
                            End If
                        End If
                    Next
                  
    'Get Profile Info...
    
                    '***FILTER PROFILE INFO***
                   
    
                 
    
                    '***FORMAT SENTENCE***
                    
                    'Write it
                    Label1.Text = Sentence
    
                    spkr = New SpeechLib.SpVoice
                    spkr.Speak(Label1.Text, SpeechLib.SpeechVoiceSpeakFlags.SVSFlagsAsync)
    
    
                    CountDown.Reset() : CountDown.Start()
    
                Else
    
                    Label2.Text = ts.Subtract(CountDown.Elapsed).ToString.Substring(0, 8) 'show time remaining
    
                End If
            End If
        End Sub
    End Class
    A large portion of this code has been omitted to protect the innocent.
    Anyone who does not wonder, is either omnipotent or a fool.
    Amerigoware <<<My Projects

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Recurring Random Countdown

    You are declaring a New Random inside of a sub. That's most likely the problem. Just use the single instance declared once on the form (which you also have). The problem with creating new Random objects is that they get seeded with the system time at a fairly coarse resolution. This means that it is entirely possible to re-create the Random object before the time has advanced, which means that the new Random object will produce the exact same sequence of numbers as the original one.

    Edit: On the other hand, I don't see any description of what the problem is, so I'm not sure that I addressed the correct issue, though what I mentioned certainly could become an issue.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member Amerigo's Avatar
    Join Date
    Dec 2008
    Location
    PSR B1620-26 c-1
    Posts
    126

    Question Re: Recurring Random Countdown

    The problem is: It isn't working (you guessed right). Without the second one, it doesn't retrieve the values of Min and Max so they remain zero, and it goes crazy. The 10, 30 is there for testing purposes.
    Also, without the second one, the recurring loops use the original random number (10-30) instead of randomizing each time through.

    Removing the first one, makes it not countdown at all.

    This is very difficult to explain. Basically, CountDownFrom is supposed to become a new number each time, and the countdown timer use that number each time, and it's not doing it.

    Btw, it's good to see you again, Shaggy.
    Anyone who does not wonder, is either omnipotent or a fool.
    Amerigoware <<<My Projects

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Recurring Random Countdown

    And you.

    After looking the code over a bit more, it is likely ok to have that second Random where it is, but why play with fire? There's no problem having two, but it is always safer to have them declared outside of the same class....except, if you actually have two created when you start the form, then they will be seeded with the same start time, and will generate the same sequence of numbers.

    My second concern is that your timer interval is very fast, yet you do lots of file access and string manipulations in the timer tick event. I'm wondering whether you are running into a problem where the timer tick is firing a second time before the processing is completed for the first time. This would have totally bizarre results, most likely. You could test this by adding Timer1.Enabled = False as the first line of the Tick event, then re-enable the timer as the final line. File reads and string manipulations are not the fastest of operations, so this is worth a test, especially since it is so easy.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Lively Member Amerigo's Avatar
    Join Date
    Dec 2008
    Location
    PSR B1620-26 c-1
    Posts
    126

    Question Re: Recurring Random Countdown

    It is not my intention to have two random generators. The problem is, if there is not one at the beginning it doesn't do the first countdown. If left only at the beginning, the countdown never changes like it's supposed to.

    Omitting the second one, the program works fine as long as the 10, 20 is there (or whatever minimum and maximum) hardwired into the program. However, it is supposed to retrieve the Min and Max variables from the user preferences.

    The program is supposed to display a randomly generated sentence for a random period of time. Then start over with a new sentence for a new random time period.
    It seems as though it should work with the following, but it does not.
    Code:
    Dim Min, Max As New Integer
        Dim r As New Random
        Dim CountDownFrom As Integer
        Dim ts As New TimeSpan(0, 0, (CountDownFrom))
        Dim CountDown As New Stopwatch
    
     '***GET MIN AND MAX***
    
     CountDownFrom = r.Next(Min, Max)
    The timer display is showing -00:00:0 then slowly counts down in the negative -00:00:1, -00:00:2, ..., -00:01:0, etc.

    The timer speed hasn't seem to have any affect, but what speed would you suggest?
    Last edited by Amerigo; Oct 1st, 2009 at 02:47 PM.
    Anyone who does not wonder, is either omnipotent or a fool.
    Amerigoware <<<My Projects

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Recurring Random Countdown

    Any speed above about 40 works for the timer, but you will have problems if a second tick happens before the processing of the first tick has finished. That seems possible, considering how much you are hitting files, but the result you are describing doesn't sound right for that kind of issue.

    However, what you have stated is very interesting, and worth some further study. I have never known Random to fail, so the first thing I would do would be to put a breakpoint on this line:

    CountDownFrom = r.Next(Min, Max)

    and see what Min and Max are. It certainly sounds like they are not what you expect them to be.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Lively Member Amerigo's Avatar
    Join Date
    Dec 2008
    Location
    PSR B1620-26 c-1
    Posts
    126

    Question Re: Recurring Random Countdown

    I have done that already. They do show what they should. It's just not getting to the timer display.
    Anyone who does not wonder, is either omnipotent or a fool.
    Amerigoware <<<My Projects

  8. #8

    Thread Starter
    Lively Member Amerigo's Avatar
    Join Date
    Dec 2008
    Location
    PSR B1620-26 c-1
    Posts
    126

    Re: Recurring Random Countdown

    I still need help with this, please.
    Anyone who does not wonder, is either omnipotent or a fool.
    Amerigoware <<<My Projects

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Recurring Random Countdown

    At this point, I'm not sure what the actual problem is. You seem to have bounced from one problem to another, and when you say that it is "not getting to the timer display", I'm not sure what that means. I guess what I'd need to understand is what you expect it to do, what it is doing, and how they are different (though that last one will probably be obvious).
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Lively Member Amerigo's Avatar
    Join Date
    Dec 2008
    Location
    PSR B1620-26 c-1
    Posts
    126

    Question Re: Recurring Random Countdown

    What it should do:
    Show a sentence for a random time period (between Min and Max). At the end of the countdown, show another sentence for a new random time period.

    What is doing:
    Showing the sentence, but not counting down properly. It is not getting/updating the variables as it should. Or, it's not displaying the countdown correctly anyway.

    I don't know how to explain it. The best thing to do, is run the program and see what it is doing. Then what it should be doing will be obvious.

    "If Dim CountDownFrom As Integer" is changed to "Dim CountDownFrom As Integer = r.Next(10, 20)"
    and
    "countdownfrom = r.next(Min, Max) is removed
    then it works, except that the countdown variables to not change as they should.
    Anyone who does not wonder, is either omnipotent or a fool.
    Amerigoware <<<My Projects

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Recurring Random Countdown

    Took me a bit, but I think I see it. The problem is here:

    Dim CountDownTo As Integer = r.Next(10, 20)
    Dim ts As New TimeSpan(0, 0, (CountDownTo))

    I haven't worked with timespans, but what these two lines do is pick a random value from 10 to 20, then create a timespan object with that value. From your description, you want that TimeSpan value to change whenever CountDownTo changes. You will have to create a new TimeSpan every time you change CountDownTo, unless the TimeSpan has a property that allows you to set whatever that argument is dynamically (the same way that you can change the interval of a timer object).

    In this code:
    Code:
    Dim CountDownFrom As Integer
        Dim ts As New TimeSpan(0, 0, (CountDownFrom))
        Dim CountDown As New Stopwatch
    
     '***GET MIN AND MAX***
    
     CountDownFrom = r.Next(Min, Max)
    You create the CountDownFrom variable, which begins with the value 0, then you create the TimeSpan, passing it the arguments (0,0,0), and only after that do you get a random value for CountDownFrom. However, by then, the TimeSpan has already been set, and it doesn't change just because you change CountDownFrom.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    Lively Member Amerigo's Avatar
    Join Date
    Dec 2008
    Location
    PSR B1620-26 c-1
    Posts
    126

    Re: Recurring Random Countdown

    That all makes sense, but what is the cure?
    Anyone who does not wonder, is either omnipotent or a fool.
    Amerigoware <<<My Projects

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Recurring Random Countdown

    Well, I'm not absolutely certain, since I don't know the specifics of the TimeSpan object, but it appears that if you just change the declaration to this:

    Dim ts as TimeSpan

    so that you aren't creating anything that will be immediately destroyed (though it would do no harm to leave it as you have it).

    The second step would be to put:

    ts = New TimeSpan(0, 0, CountDownFrom)

    immediately after you set CountDownFrom, so that it looks like this:
    Code:
    CountDownFrom = r.Next(Min, Max)
    ts = New TimeSpan(0, 0, CountDownFrom)
    Or perhaps that should be CountDownTo, which is the line that appears to be commented out in your initial post.
    My usual boring signature: Nothing

  14. #14

    Thread Starter
    Lively Member Amerigo's Avatar
    Join Date
    Dec 2008
    Location
    PSR B1620-26 c-1
    Posts
    126

    Re: Recurring Random Countdown

    I changed it to CountDownFrom, because that makes more sense.

    I tried what you suggested already. That made the program go crazier than ever. It just displayed rapidly changing random times without ever counting down.

    I will try it without the parameters in the first one.

    I'm wondering if some kind of threading will be needed here. Unfortunately, I don't know a thing about threading.
    Anyone who does not wonder, is either omnipotent or a fool.
    Amerigoware <<<My Projects

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Recurring Random Countdown

    Yeah, I missed an item. I didn't see that you called WriteLine soon after creating the timespan. That's going to take a little thought, but it won't require multithreading.

    The issue is this: By doing what I suggested, the timespan changes every time the timer ticks, because the CountDownFrom changes every time the timer ticks. I'm pretty sure that is NOT what you want. By having these two lines:

    CountDownFrom = r.Next(Min, Max)
    ts = New TimeSpan(0, 0, CountDownFrom)

    inside the timer tick event, then every time the timer ticks, the CountDownFrom is changed to a different random value, and the timespan is also changed. Those lines have to be removed from the timer tick event, but where should they go? Basically, you want to have those two lines right before you ever start the timer, and right before you ever re-start the timer. Hopefully, that will suggest a location to you, or perhaps two locations. I noticed some calls to Reset and the like, so it may be that there is more than one place to put those lines.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width