CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Guest

    How can I Randomize a list of text words

    I need the code so that I can have a list of names (text) that will randomly pick one of those and display it in the assigned textbox, please help!! Stuck!


  2. #2
    Guest

    Re: How can I Randomize a list of text words

    An idea...
    Put the words into an array and while you're in a loop
    get two random indexes and swap the elements accordingly.
    Let it loop some times, it depends on the number of words.
    Then you have a random list of words.
    All you have to do now, is to get element by element from
    the first to the last.




  3. #3
    Join Date
    Aug 2008
    Posts
    2

    Talking Re: How can I Randomize a list of text words

    in my way of doing it

    you need to have two listboxes

    the first one will have the list of the original values meaning the raw list of data then the second listbox will have the randomize jumbled list of data

    sub randomizeList()
    dim list1Len,i as integer

    list1Len = list1.ListCount

    while list1.listcount >0
    rndIndex = Rnd * List1.Listcount
    list2.additem list1.list(rndIndex)
    list1.removeitem rndIndex
    wend
    end sub

    then to avoid having the same set of random data

    private sub form_activate()
    randomize timer
    end sub

    give me feedback if ever you have problems

    can't guarantee your satisfaction

    i am just a student here

    may e-add is [email protected]

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: How can I Randomize a list of text words

    Having the text items in a ListBox is not a bad idea, because you can make it visible during program developement and see what's in there.
    Assuming List1 to contain your collection of texts, txtRand being the TextBox for the result, you select an item randomly to be displayed like this:
    Code:
    Private Sub btnRand_Click()
      txtRand.Text = List1.List(Int(Rnd(1) * List1.ListCount))
    End Sub
    I put it into a command button click to test it. Works fine.

    Have to add: the randomize timer statement nekoanz mentioned, is a good idea to create a different random sequence at every program start
    Last edited by WoF; February 10th, 2009 at 10:21 AM.

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: How can I Randomize a list of text words

    This thread is almost 10 years old... I would think that the problem has been long solved..

    nekoanz, I respect that you were trying to help someone out with your first post, however try to answer the more pressing questions (ie. those in the last few days).....

    Gremmy..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: How can I Randomize a list of text words

    Really! I think I'd better start looking at the date stamp of the original post.

  7. #7
    Join Date
    Aug 2008
    Posts
    2

    Talking Re: How can I Randomize a list of text words

    i overlooked the date part i am so sorry but i am not the only that is being fooled by the date

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

Featured