VB6 Random always the same sequence

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Wernerh
    New Member
    • Jul 2007
    • 104

    VB6 Random always the same sequence

    I have a random function built into my application. It works perfectly. Just one problem it runs the exact random path every time you start the application. Therefore the random selection kicks out the same sequence every time. Is there anything else I can add to the code to overcome that problem? Hope it makes sense.

    (Code)
    Dim Q
    Q = Int((68 * Rnd) + 1)
    Text5.Text = Q

    Thanks
  • hariharanmca
    Top Contributor
    • Dec 2006
    • 1977

    #2
    Originally posted by Wernerh
    I have a random function built into my application. It works perfectly. Just one problem it runs the exact random path every time you start the application. Therefore the random selection kicks out the same sequence every time. Is there anything else I can add to the code to overcome that problem? Hope it makes sense.

    (Code)
    Dim Q
    Q = Int((68 * Rnd) + 1)
    Text5.Text = Q

    Thanks
    Use one timer control and intrvel to 20 or 50

    then use the below code

    [CODE=vb]Dim q As Double

    Private Sub Command1_Click( )
    Text2.Text = Int((68 * q) + 1)
    End Sub

    Private Sub Timer1_Timer()
    q = Rnd()
    End Sub

    [/CODE]

    I think this will help you.
    Last edited by hariharanmca; Aug 30 '07, 11:45 AM. Reason: Changed code.

    Comment

    • Wernerh
      New Member
      • Jul 2007
      • 104

      #3
      Thanks,

      I will give it a go and will let you know

      Cheers

      Comment

      • Tig201
        New Member
        • Mar 2007
        • 103

        #4
        or you could just place the following in your form load
        [CODE=vb]call randomize[/CODE]

        Comment

        • hariharanmca
          Top Contributor
          • Dec 2006
          • 1977

          #5
          Originally posted by Tig201
          or you could just place the following in your form load
          [CODE=vb]call randomize[/CODE]
          Can you explain with example. Because its returning same sequence.

          Comment

          • Wernerh
            New Member
            • Jul 2007
            • 104

            #6
            I have used your code as you have suggested and it does not work :-<< It now does not random select at all, it brings up the same selection on every code execution. The list has 68 random selection available. It chose a diff first item than what it use to, but now will not select a second item on execution. Selects same one over and over?

            Comment

            • Wernerh
              New Member
              • Jul 2007
              • 104

              #7
              Yipee

              call randomize Works like a charm!

              Thank you

              Comment

              • hariharanmca
                Top Contributor
                • Dec 2006
                • 1977

                #8
                Originally posted by Wernerh
                I have used your code as you have suggested and it does not work :-<< It now does not random select at all, it brings up the same selection on every code execution. The list has 68 random selection available. It chose a diff first item than what it use to, but now will not select a second item on execution. Selects same one over and over?
                I think you took my older version (later i edited). Just try this. (i missed that rnd method)

                [CODE=vb]Dim q As Double

                Private Sub Command1_Click( )
                Text2.Text = Int((68 * q) + 1)
                End Sub

                Private Sub Timer1_Timer()
                q = Rnd()
                End Sub[/CODE]

                if code working fine, then no problem.

                Comment

                • Wernerh
                  New Member
                  • Jul 2007
                  • 104

                  #9
                  Thanks for the help, yes the call randomize was all that was needed. Thanks anyway

                  Comment

                  • hariharanmca
                    Top Contributor
                    • Dec 2006
                    • 1977

                    #10
                    Originally posted by Tig201
                    or you could just place the following in your form load
                    [CODE=vb]call randomize[/CODE]
                    Yes, this is also working fine. Good.

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by Wernerh
                      Thanks for the help, yes the call randomize was all that was needed. Thanks anyway
                      Just a tip - you can drop the Call and just code Randomize.

                      Comment

                      • Tig201
                        New Member
                        • Mar 2007
                        • 103

                        #12
                        Originally posted by hariharanmca
                        Can you explain with example. Because its returning same sequence.
                        For testing purposes (or at least this is what I was taught) the RND() function goes in a set order so it will give the same numbers every time it is run until you use Randomize.


                        Originally posted by Killer42
                        Just a tip - you can drop the Call and just code Randomize.
                        Yes, you can but I was taught to use the keyword Call in school and it has become habit.

                        Comment

                        • sgrec7
                          New Member
                          • Aug 2007
                          • 58

                          #13
                          Originally posted by Wernerh
                          I have a random function built into my application. It works perfectly. Just one problem it runs the exact random path every time you start the application. Therefore the random selection kicks out the same sequence every time. Is there anything else I can add to the code to overcome that problem? Hope it makes sense.

                          (Code)
                          Dim Q
                          Q = Int((68 * Rnd) + 1)
                          Text5.Text = Q

                          Thanks
                          Seriously guys I'm a n00b and I know this stuff.

                          This is what you want

                          [code=vb]

                          private sub (what ever)

                          dim Q as integer

                          Randomize (this is the peice that you are missing)

                          Q = int (rnd * 68) +1

                          text5.text = Q

                          end sub

                          [/code]

                          by the way "randomize" doesn't go blue

                          so yea this will definitely work

                          sgrec7

                          Comment

                          Working...