Results 1 to 7 of 7

Thread: [RESOLVED] Random Number

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Resolved [RESOLVED] Random Number

    Having trouble figuring out how to make this code work. I want a random number between 1-3, then depending on the outcome, I want it to select one of three radio buttons. This is the code I have so far minus the random number:

    Code:
    For Each ele As HtmlElement In WebWindow.WebBrowser1.Document.All
                            If ele.GetAttribute("id").ToLower = "RadioGroup1".ToLower Then
                                ele.InvokeMember("click")
                            End If
                        Next
    Firstly, is there an easier way to code that? Second, I'm using that code over and over. Is there a way to turn that into a function and input the name where I need it, like WEBELEMENT(RadioGroup_02)?

    Third, so I'm using this code to get a random number between 1-3:

    Code:
    Dim rando As String = CInt(Int((3 - 1 + 1) * Rnd() + 1))
    First, is there a shorter way to code that? Second, how might I combine the two codes so when rando = 1, the program does RadioGroup1, rando = 2, RadioGroup2, rando = 3, RadioGroup3? I'm sure there are so many ways to do this, but I'm having difficulty thinking of a way to make this work - just having that overwhelmed feeling and I don't know why. Sorry, that was a bit ot. Any ideas welcome.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Re: Random Number

    Actually, I'm going to try this. But I'm sure there is a cleaner way to code this that I'm missing.

    Code:
    Dim rando As String = CInt(Int((3 - 1 + 1) * Rnd() + 1))
    
                    If rando = 1 Then
    
                        For Each ele As HtmlElement In WebWindow.WebBrowser1.Document.All
                            If ele.GetAttribute("id").ToLower = "RadioGroup0".ToLower Then
                                ele.InvokeMember("click")
                            End If
                        Next
                    End If
    
                    If rando = 2 Then
    
                        For Each ele As HtmlElement In WebWindow.WebBrowser1.Document.All
                            If ele.GetAttribute("id").ToLower = "RadioGroup1".ToLower Then
                                ele.InvokeMember("click")
                            End If
                        Next
                    End If
    
                    If rando = 3 Then
    
                        For Each ele As HtmlElement In WebWindow.WebBrowser1.Document.All
                            If ele.GetAttribute("id").ToLower = "RadioGroup2".ToLower Then
                                ele.InvokeMember("click")
                            End If
                        Next
                    End If

  3. #3
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Random Number

    Hi,

    To begin with you should be using the Random Class in .NET rather than the legacy Rnd function that was introduced with earlier versions of VB. Using this calss, here is a streamlined version of your code:-

    Code:
    Dim myRandom As New Random
     
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
      Dim strSelectedGroup As String = "radiogroup" & myRandom.Next(0, 3).ToString
     
      For Each ele As HtmlElement In WebWindow.WebBrowser1.Document.All
        If ele.GetAttribute("id").ToLower = strSelectedGroup Then
          ele.InvokeMember("click")
        End If
      Next
    End Sub
    Hope that helps.

    Cheers,

    Ian

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Re: Random Number

    See, I knew there was an easier way! To do both of what I wanted! I thank you sir! That's just what I needed.

  5. #5
    Addicted Member MetalInquisitor's Avatar
    Join Date
    Sep 2012
    Posts
    143

    Re: [RESOLVED] Random Number

    Also, keep in mind that the first number you provide to Random.Next is inclusive, but the second one is not; so if you want a random number between 1 and 3, you should generate it like this:

    vb.net Code:
    1. myRandom.Next(1, 4)

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Re: [RESOLVED] Random Number

    EDIT: Should I have started a new post for this, since I had already marked this one as Resolved? Bummer I can't Unresolve it.

    Alright, I finally get everything flowing smoothly and my code is about 2/3 smaller, so I'm feeling good, however slight hiccup. In my old code, I had it so that there was a 3/4th chance of selecting RadioButton0 and a 1/4th chance of selecting RadioButton1. By itself, this code gives a 50% chance:

    Code:
    Dim stSuper As String = "radiogroup_2" & rand.Next(0, 2).ToString
    
                    For Each ele As HtmlElement In WebWindow.WebBrowser1.Document.All
                        If ele.GetAttribute("id").ToLower = stSuper Then
                            ele.InvokeMember("click")
                        End If
                    Next
    So I tried this:

    Code:
     Dim OneInFour As String = rand.Next(1, 5).ToString
                    If OneInFour = 1 Then Dim stSuper As String = "radiogroup_20".ToString Else 
                    If OneInFour <> 1 Then Dim stSuper As String = "radiogroup_2" & rand.Next(0, 2).ToString
                    For Each ele As HtmlElement In WebWindow.WebBrowser1.Document.All
                        If ele.GetAttribute("id").ToLower = stSuper Then
                            ele.InvokeMember("click")
                        End If
                    Next
    Which is giving me the error:

    Variable stSuper hides a variable in an enclosing block
    Any idea's how to resolve this? And one more thing while it's on my mind; if I turn OneInFour into a Sub, will it randomize every time I call it, or would I have basically just Dim a new OneInFour for every iteration I want?

  7. #7
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: [RESOLVED] Random Number

    Hi,

    Variable stSuper hides a variable in an enclosing block
    To specifically answer your question, the reason you are getting this error is due to fact that you are declaring the variable within the If statements and therefore the scope of this variable is only visible within the If blocks and not outside. To solve this you would declare the variable outside of the If block. i.e:-

    Code:
    Dim OneInFour As String = rand.Next(1, 5).ToString
    Dim stSuper As String
     
    If OneInFour = "1" Then
      stSuper = "radiogroup_20"
    Else
      stSuper = "radiogroup_2" & rand.Next(0, 2).ToString
    End If
     
    For Each ele As HtmlElement In WebWindow.WebBrowser1.Document.All
      If ele.GetAttribute("id").ToLower = stSuper Then
        ele.InvokeMember("click")
      End If
    Next
    That said, and if I understand your comments correctly, this is not going to do what you want since you want 1 in 4 chances of picking RadionButton20 and 3 in 4 chances of picking RadioButton21. If I am correct then you would want to say:-

    Code:
    Dim OneInFour As String = rand.Next(1, 5).ToString
    Dim stSuper As String
    
    If OneInFour = "1" Then
      stSuper = "radiogroup_20"
    Else
      stSuper = "radiogroup_21"
    End If
    And one more thing while it's on my mind; if I turn OneInFour into a Sub, will it randomize every time I call it, or would I have basically just Dim a new OneInFour for every iteration I want?
    If you put this into a Sub then you would still need to call rand.Next to get any random number in the range that you want.

    In addition to this, turn Option Strict On and keep it on. It will help you to eradicate type errors in the future. I have fixed these in the examples shown above.

    Hope that helps.

    Cheers,

    Ian

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