Results 1 to 7 of 7

Thread: Randomize strings?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2000
    Posts
    5

    Post

    I have to randomize 7 strings in my application. And I do know how to randomize *******s but not how to randomize strings. I also wounder how to check that the string don't come twice. Please help me this.

  2. #2
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Post

    Try this:
    Code:
    Public Function RandomString() as String
        ReDim strArray(0 to 6) as string
        Dim i as integer
        ' Fill String Array
        For i = 0 to 6
            strArray(i) = "String number " & (i + 1)
        Next
        Randomize Timer
        ' Get Random Array Index
        i = Int(Rnd * 7)
        ' Set Function equal to random string
        RandomString = strArray(i)
    End Function
    And then you call it with:
    Code:
        'Set Label1.Text equal to Random String
        Label1.Text = RandomString()

    r0ach™
    Don't forget to rate the post

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2000
    Posts
    5

    Post

    weell.. when does the function randomize the strings? if my string name is frstr1 and frstr2 etc. when where shall i put their names?

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2000
    Posts
    5

    Post

    weell.. when does the function randomize the strings? if my string name is frstr1 and frstr2 etc. when where shall i put their names?

  5. #5
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Post

    Replace the For loop in the RandomString Function with:
    Code:
        strArray(0) = frstr1
        strArray(1) = frstr2
        strArray(2) = frstr3
        strArray(3) = frstr4
        strArray(4) = frstr5
        strArray(5) = frstr6
        strArray(6) = frstr7
    This is how it works:
    You generate a random integer (like you already know how to ) and then use it as an Index for an array holding your strings

    r0ach™
    Don't forget to rate the post

  6. #6
    Guest

    Post

    Do you currently have your strings in an array?

    Are they generated strings and there is only seven or is there a lot more strings and you only want to choose seven of them?

    Roach is right about indexing, but you need your strings to be in an array.

    If you create a duplicate array defined as boolean, when you use the string you can set the indexed boolean to true and then when you generate your random index check the boolean value for true and if it is generate a different index. Or you could loop through the previously retrieved strings and see if they match.

    If you are looking for a code solution, I need more information.

  7. #7
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Post

    Uhmmm. It would be even better if you assign the Strings to the array in the Form_Load() and not in the RandomString() like i did.

    r0ach™
    Don't forget to rate the post

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