Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Random method

Random method

Scheduled Pinned Locked Moved C#
data-structureshelplounge
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MorganSim
    wrote on last edited by
    #1

    Trying to use the random() to generate random words from a string[],now I want to put the already picked words in may be an array so that the random() should not pick that word if once picked before.please help

    B G 2 Replies Last reply
    0
    • M MorganSim

      Trying to use the random() to generate random words from a string[],now I want to put the already picked words in may be an array so that the random() should not pick that word if once picked before.please help

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      Random helper class...[^]


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

      M G 2 Replies Last reply
      0
      • M MorganSim

        Trying to use the random() to generate random words from a string[],now I want to put the already picked words in may be an array so that the random() should not pick that word if once picked before.please help

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        If you do that, you have to loop when you try to find an unused word. Instead, put the words in a list, and remove the words that are picked:

        // create the list
        List<string> words = new List<string>(arrayOfWords);
        // create a random object to use
        Random rnd = new Random();

        // pick one item from the list
        int index = rnd.Next(words.Count);
        // get the string
        string pickedWord = words[index];
        // remove the picked item from the list
        words.RemoveAt(index);

        Despite everything, the person most likely to be fooling you next is yourself.

        modified on Sunday, September 7, 2008 11:17 AM

        M 1 Reply Last reply
        0
        • B Blue_Boy

          Random helper class...[^]


          I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

          M Offline
          M Offline
          MorganSim
          wrote on last edited by
          #4

          Thats was really a great prog. Now check here, am generating a random word from an array which I display on the textbox in form of stars for the user to guess.Now I don't want the user to come across the same word again.How can I please go about it to make the random method not to get the same word again.Like the way Hung man game operates waiting......

          1 Reply Last reply
          0
          • G Guffa

            If you do that, you have to loop when you try to find an unused word. Instead, put the words in a list, and remove the words that are picked:

            // create the list
            List<string> words = new List<string>(arrayOfWords);
            // create a random object to use
            Random rnd = new Random();

            // pick one item from the list
            int index = rnd.Next(words.Count);
            // get the string
            string pickedWord = words[index];
            // remove the picked item from the list
            words.RemoveAt(index);

            Despite everything, the person most likely to be fooling you next is yourself.

            modified on Sunday, September 7, 2008 11:17 AM

            M Offline
            M Offline
            MorganSim
            wrote on last edited by
            #5

            That was quiet penetrating, which namespace do I use for Lists. Is it an ArrayList or SortedList you are talking about(am familiar with these System.Collections;, System.Text;)? :((

            G 1 Reply Last reply
            0
            • B Blue_Boy

              Random helper class...[^]


              I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Read the question before you reply. It's not helpful to post your self-promoting links if they don't apply to the question at all.

              Despite everything, the person most likely to be fooling you next is yourself.

              1 Reply Last reply
              0
              • M MorganSim

                That was quiet penetrating, which namespace do I use for Lists. Is it an ArrayList or SortedList you are talking about(am familiar with these System.Collections;, System.Text;)? :((

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                The List<T> class is in the System.Collections.Generic namespace. Unless you are stuck with framework 1.x you should not use ArrayList at all.

                Despite everything, the person most likely to be fooling you next is yourself.

                M 1 Reply Last reply
                0
                • G Guffa

                  The List<T> class is in the System.Collections.Generic namespace. Unless you are stuck with framework 1.x you should not use ArrayList at all.

                  Despite everything, the person most likely to be fooling you next is yourself.

                  M Offline
                  M Offline
                  MorganSim
                  wrote on last edited by
                  #8

                  Thanks. Unfortunatly, am only able to program on one form.How about having multiple forms and calling them interchangebly.

                  G 1 Reply Last reply
                  0
                  • M MorganSim

                    Thanks. Unfortunatly, am only able to program on one form.How about having multiple forms and calling them interchangebly.

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    MorganSim wrote:

                    Unfortunatly, am only able to program on one form.How about having multiple forms and calling them interchangebly.

                    I don't see how this relates to the original question at all? Can you clearify?

                    Despite everything, the person most likely to be fooling you next is yourself.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups