How to select randomly Single String from multiple Strings in VB Script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davidson1
    New Member
    • Feb 2008
    • 144

    How to select randomly Single String from multiple Strings in VB Script

    Hi Friends,

    I am trying in VBScript. I have names "Orange","Red", "Blue","Yellow" . I need any one of this names to be displayed randomly in MSGBOX. How can I do that. I tried the below code but it is not working. Can you correct it if it is wrong.

    Code:
     <html>
    <head>
    <script type="text/vbscript">
    
    Dim first() as String = {"Orange","Red","Blue","Yellow"}
    MsgBox(first(Rnd))
    
    </script>
    </head>
    </html>
  • yarbrough40
    Contributor
    • Jun 2009
    • 320

    #2
    the Rnd function only returns a number from 0 to 1. try something like:

    Code:
    Dim first() As String = {"Orange", "Red", "Blue", "Yellow"}
            Dim random As New Random()
            MsgBox(first(random.Next(4)))

    Comment

    • davidson1
      New Member
      • Feb 2008
      • 144

      #3
      I tried this below Code..But it is not working..Any other alternate code

      [CODE]

      <html>
      <head>
      <script type="text/vbscript">

      Dim first() As String = {"Orange", "Red", "Blue", "Yellow"}
      Dim random As New Random()
      MsgBox(first(ra ndom.Next(4)))

      </script>
      </head>
      </html>

      [\CODE]

      Comment

      • yarbrough40
        Contributor
        • Jun 2009
        • 320

        #4
        try
        Code:
        Dim first() As String = {"Orange", "Red", "Blue", "Yellow"}
        Dim Random As Integer = CInt(Int((4 * Rnd()) + 0))
        MsgBox(first(Random))

        Comment

        • davidson1
          New Member
          • Feb 2008
          • 144

          #5
          I tried this below code. But still it is not working. Any other code?

          [CODE]
          <html>
          <head>
          <script type="text/vbscript">
          Dim first() As String = {"Orange", "Red", "Blue", "Yellow"}
          Dim Random As Integer = CInt(Int((4 * Rnd()) + 0))
          MsgBox(first(Ra ndom))
          </script>
          </head>
          </html>
          [\CODE]

          Comment

          • yarbrough40
            Contributor
            • Jun 2009
            • 320

            #6
            can you be more specific as opposed to just "it's not working"? : ) are you getting compilation errors / runtime errors? if so what do they say?

            I am a vb.net guy so you may just need to do some googling for "vbscript random function"

            Comment

            • davidson1
              New Member
              • Feb 2008
              • 144

              #7
              The message box is not coming with that Random name. It just executes and shows a Blank Page.No Error.

              Comment

              Working...