Random labels.text

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cindy2
    New Member
    • Sep 2007
    • 35

    Random labels.text

    Hi everyone,

    I have 4 textboxes on form1. On runtime the user will be asked to fill in names. After that, I want these names placed RANDOMLY on 4 labels (form2). How should I do that? I am trying it for ages but I still don't have a clue to get them random.

    Please help me.

    Cindy
  • VBPhilly
    New Member
    • Aug 2007
    • 95

    #2
    Originally posted by cindy2
    Hi everyone,

    I have 4 textboxes on form1. On runtime the user will be asked to fill in names. After that, I want these names placed RANDOMLY on 4 labels (form2). How should I do that? I am trying it for ages but I still don't have a clue to get them random.

    Please help me.

    Cindy
    Random is about numbers in vb6.

    You should represent your labels as a number, and ask for one of the numbers in a random number generator function.

    So, for each textbox. You could say, "RandomNumberGe nerator function, give me a number between 1 and 4."

    For example,
    It returns 3.

    See if the 3 label has a value, if not put the name there (from the textbox).

    If it does have a value, keep askin the RandomNumberGen erator to give you another number until you get a clean label to populate.

    keep going until you get your labels filled (all 4 of them).

    Your technical challenge is with the Random generator function. You'll need to make it give you a value of only between 1 and 4. Let me know if you need help with this.

    Comment

    • cindy2
      New Member
      • Sep 2007
      • 35

      #3
      Originally posted by VBPhilly
      Random is about numbers in vb6.

      You should represent your labels as a number, and ask for one of the numbers in a random number generator function.

      So, for each textbox. You could say, "RandomNumberGe nerator function, give me a number between 1 and 4."

      For example,
      It returns 3.

      See if the 3 label has a value, if not put the name there (from the textbox).

      If it does have a value, keep askin the RandomNumberGen erator to give you another number until you get a clean label to populate.

      keep going until you get your labels filled (all 4 of them).

      Your technical challenge is with the Random generator function. You'll need to make it give you a value of only between 1 and 4. Let me know if you need help with this.

      Thank you for your help!

      I don't know if this is the most elegant way (probebly not :)), but it does work. I have something that looks like a random generator:

      My module1 looks like this:

      Function random(ByVal x As Single) As Single
      Dim y As Single
      x = Rnd()
      If x < 0.25 Then
      y = 1
      ElseIf x < 0.5 And 0.25 < x Then
      y = 2
      ElseIf x < 0.75 And 0.5 < x Then
      y = 3
      ElseIf x < 1 And 0.75 < x Then
      y = 4
      End If
      random = y
      End Function


      And my form1 (button_click) looks like this:

      Dim g As Integer
      Form2.Show()
      Label1.Text = random(g)


      But how can I represent my labels as numbers. Do I have to rename my labels as numbers?

      I don't know how to connect the generator with the textboxes and labels.


      Cindy

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        If you have a look at the index in the VB Articles area, there's a routine there to return a random whole-number value within specified limits.

        Comment

        • cindy2
          New Member
          • Sep 2007
          • 35

          #5
          Originally posted by Killer42
          If you have a look at the index in the VB Articles area, there's a routine there to return a random whole-number value within specified limits.
          Ok Thanks, the Randomize function is a lot easyer than my original generator. So I have the next code:


          ' Initialize the random-number generator.
          Randomize()
          ' Generate random value between 1 and 4.
          Dim value As Integer = CInt(Int((4 * Rnd()) + 1))

          Label1.Text = value

          But how do I connect this with my labels and textboxes. It is possible to do it with an If-Then structure. But that is a lot of programming. Is there maybe a better way?

          Cindy

          Comment

          • kadghar
            Recognized Expert Top Contributor
            • Apr 2007
            • 1302

            #6
            Originally posted by cindy2
            Ok Thanks, the Randomize function is a lot easyer than my original generator. So I have the next code:


            ' Initialize the random-number generator.
            Randomize()
            ' Generate random value between 1 and 4.
            Dim value As Integer = CInt(Int((4 * Rnd()) + 1))

            Label1.Text = value

            But how do I connect this with my labels and textboxes. It is possible to do it with an If-Then structure. But that is a lot of programming. Is there maybe a better way?

            Cindy
            work it with arrays, the main idea would be something like this

            [CODE=vb]dim myArr(1 to 4) as string 'the ones in textboxes
            dim myArr2(1 to 4) as string 'the ones in labels
            dim Order(1 to 4) as integer
            dim i as integer
            dim j as integer

            order(1) = int(rnd * 4) +1
            i=2
            for i = 2 to 4
            order(i) = int(rnd * 4)+1
            j=1
            do
            if order(i) = order(j) then
            order(i) = int(rnd * 4)+1
            j = 1
            else
            j=j+1
            if j = i then exit do
            end if
            loop
            i=i+1
            next

            for i = 1 to 4
            myarr2(i) = myarr(order(i))
            next[/CODE]

            then just write the second array in your labels

            Comment

            • VBPhilly
              New Member
              • Aug 2007
              • 95

              #7
              How can you represent labels as numbers? Give the labels the same name.

              Each label will then be indexed because they will be considered a control array.

              You'll then refer to the labels as:

              Label(0)
              Label(1)
              Label(2)
              Label(3)

              From above, you can see how you would refer to the labels as a number. 0, 1, 2 or 3.

              Comment

              • cindy2
                New Member
                • Sep 2007
                • 35

                #8
                Originally posted by kadghar
                work it with arrays, the main idea would be something like this

                [CODE=vb]dim myArr(1 to 4) as string 'the ones in textboxes
                dim myArr2(1 to 4) as string 'the ones in labels
                dim Order(1 to 4) as integer
                dim i as integer
                dim j as integer

                order(1) = int(rnd * 4) +1
                i=2
                for i = 2 to 4
                order(i) = int(rnd * 4)+1
                j=1
                do
                if order(i) = order(j) then
                order(i) = int(rnd * 4)+1
                j = 1
                else
                j=j+1
                if j = i then exit do
                end if
                loop
                i=i+1
                next

                for i = 1 to 4
                myarr2(i) = myarr(order(i))
                next[/CODE]

                then just write the second array in your labels

                Thank you, it looks very nice. It took a while, but now I understand your programming: It fils the (Order)Array, element by element, until the whole array is filt with different numbers (1 to 4). I only don't understand the next code:

                myArr2(i) = myArr(Order(i))

                If I run the programm, I will get the following error: "Index was outside the bounds of the array." Maybe this is because of the adjustments I had to make for the arrays. Arrays should always begin with the 0-ellement. So my code looks like this:

                [CODE=vb]Dim myArr(0 To 3) As String 'the ones in textboxes
                Dim myArr2(0 To 3) As String 'the ones in labels
                Dim Order(0 To 3) As Integer
                Dim i As Integer
                Dim j As Integer

                Order(0) = Int(Rnd() * 4) + 1
                i = 1
                For i = 1 To 3
                Order(i) = Int(Rnd() * 4) + 1
                j = 0
                Do
                If Order(i) = Order(j) Then
                Order(i) = Int(Rnd() * 4) + 1
                j = 0
                Else
                j = j + 1
                If j = i Then Exit Do
                End If
                Loop
                i = i + 1
                Next

                For i = 0 To 3
                myArr2(i) = myArr(Order(i))
                Next[/CODE]

                Apart form the error I get, I really wanna know what the code

                myArr2(i) = myArr(Order(i))

                exactly means. Because if I don't understand it, I can't make it work.

                Cindy

                Comment

                • kadghar
                  Recognized Expert Top Contributor
                  • Apr 2007
                  • 1302

                  #9
                  Originally posted by cindy2
                  Thank you, it looks very nice. It took a while, but now I understand your programming: It fils the (Order)Array, element by element, until the whole array is filt with different numbers (1 to 4). I only don't understand the next code:

                  myArr2(i) = myArr(Order(i))

                  If I run the programm, I will get the following error: "Index was outside the bounds of the array." Maybe this is because of the adjustments I had to make for the arrays. Arrays should always begin with the 0-ellement. So my code looks like this:
                  Yeap, not all the VBs let you use arrays starting in 1.

                  here you have 3 arrays:

                  MyArr should look like this:
                  MyArr(0) = John
                  MyArr(1) = Peter
                  MyArr(2) = Bill
                  MyArr(3) = Bob

                  Order array has random integers, but it could look like this:
                  Order(0) = 3
                  Order(1) = 1
                  Order(2) = 4
                  Order(3) = 2

                  and now MyArr2(order(0) = myarr(0)... that means myArr2(3) = John and so on...

                  The problem here is that Order has Integers from 1 to 4 so all you have to do is to subtract one to that index:

                  myArr2(i) = myArr(Order(i) - 1)

                  That should fix the problem
                  HTH

                  Comment

                  • cindy2
                    New Member
                    • Sep 2007
                    • 35

                    #10
                    Originally posted by kadghar
                    The problem here is that Order has Integers from 1 to 4 so all you have to do is to subtract one to that index:

                    myArr2(i) = myArr(Order(i) - 1)

                    That should fix the problem
                    HTH
                    Just before your reply I considerd also the substraction by one. But unfortunately the same error occured. :(

                    Cindy

                    Comment

                    • kadghar
                      Recognized Expert Top Contributor
                      • Apr 2007
                      • 1302

                      #11
                      Originally posted by cindy2
                      Just before your reply I considerd also the substraction by one. But unfortunately the same error occured. :(

                      Cindy
                      Hi there,

                      I've checked the code, it had some indexes troubles with the FOR since the begining, this should solve it
                      (also now Order is a random number from 0 to 3 so its not necessary to substract one anymore)
                      [CODE=vb]
                      Dim myArr(0 To 3) As String 'the ones in textboxes
                      Dim myArr2(0 To 3) As String 'the ones in labels
                      Dim Order(0 To 3) As Integer
                      Dim i As Integer
                      Dim j As Integer

                      Order(0) = Int(Rnd() * 4)

                      For i = 1 To 3
                      Order(i) = Int(Rnd() * 4)
                      j = 0
                      Do
                      If Order(i) = Order(j) Then
                      Order(i) = Int(Rnd() * 4)
                      j = 0
                      Else
                      j = j + 1
                      If j = i Then Exit Do
                      End If
                      Loop
                      Next

                      For i = 0 To 3
                      myArr2(i) = myArr(Order(i))
                      Next[/CODE]

                      HTH

                      Comment

                      • cindy2
                        New Member
                        • Sep 2007
                        • 35

                        #12
                        Originally posted by kadghar
                        I've checked the code, it had some indexes troubles ...
                        Thank You Very Much!!!!!
                        It works perfectly.

                        Cindy
                        Last edited by Killer42; Sep 18 '07, 11:03 PM. Reason: Reduced excessive quote block

                        Comment

                        Working...