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 Number Generator

Random Number Generator

Scheduled Pinned Locked Moved C#
csharpdata-structureslounge
8 Posts 6 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.
  • B Offline
    B Offline
    Belfast Child
    wrote on last edited by
    #1

    Hi, I have an array (which may change in length each time) of numbers (employeeid) and I want to create a random generator function to return one these numbers. The only parameters I will be receiving are the array of numbers and the array length. I'm new to C# so any sample code would be appreciated. Thanks.

    E 1 Reply Last reply
    0
    • B Belfast Child

      Hi, I have an array (which may change in length each time) of numbers (employeeid) and I want to create a random generator function to return one these numbers. The only parameters I will be receiving are the array of numbers and the array length. I'm new to C# so any sample code would be appreciated. Thanks.

      E Offline
      E Offline
      eggsovereasy
      wrote on last edited by
      #2

      public int GetRandomInt(int[] array, int length)
      {
      return array[new Random.Next(0, length)];
      }

      B 1 Reply Last reply
      0
      • E eggsovereasy

        public int GetRandomInt(int[] array, int length)
        {
        return array[new Random.Next(0, length)];
        }

        B Offline
        B Offline
        Belfast Child
        wrote on last edited by
        #3

        Thanks for your reply. I'm getting the follwoing error.. Error 2 'System.Random.Next()' is a 'method' but is used like a 'type' All I require is just one number returned from the array. Does return array not return a list of arrays or does the Next method refine it to one number? Pardon my ignorance.

        W L G 3 Replies Last reply
        0
        • B Belfast Child

          Thanks for your reply. I'm getting the follwoing error.. Error 2 'System.Random.Next()' is a 'method' but is used like a 'type' All I require is just one number returned from the array. Does return array not return a list of arrays or does the Next method refine it to one number? Pardon my ignorance.

          W Offline
          W Offline
          Wjousts
          wrote on last edited by
          #4

          Belfast Child wrote:

          Does return array not return a list of arrays or does the Next method refine it to one number?

          array is your variable that was passed to the method. Using the square brackets is indexing the array to retrieve one of the members of the array. Since it is an array of int, you will return an int, which is what the method signature specified.

          B 1 Reply Last reply
          0
          • B Belfast Child

            Thanks for your reply. I'm getting the follwoing error.. Error 2 'System.Random.Next()' is a 'method' but is used like a 'type' All I require is just one number returned from the array. Does return array not return a list of arrays or does the Next method refine it to one number? Pardon my ignorance.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Belfast Child wrote:

            Error 2 'System.Random.Next()' is a 'method' but is used like a 'type'

            Random r = new Random();
            r.Next(...)

            regards

            1 Reply Last reply
            0
            • W Wjousts

              Belfast Child wrote:

              Does return array not return a list of arrays or does the Next method refine it to one number?

              array is your variable that was passed to the method. Using the square brackets is indexing the array to retrieve one of the members of the array. Since it is an array of int, you will return an int, which is what the method signature specified.

              B Offline
              B Offline
              Belfast Child
              wrote on last edited by
              #6

              Cheers Guys. Figured it out. Here's a test on a click button for anyone else interested public int GetRandomInt(int[] array, int length) { Random ranNum = new Random(); return array[ranNum.Next(0, length)]; } private void button1_Click(object sender, EventArgs e) { int [] intArray; intArray = new int[3] {123, 321, 666}; int randumNum = GetRandomInt(intArray, 3); randomLabel.Text = (randumNum.ToString()); }

              S 1 Reply Last reply
              0
              • B Belfast Child

                Thanks for your reply. I'm getting the follwoing error.. Error 2 'System.Random.Next()' is a 'method' but is used like a 'type' All I require is just one number returned from the array. Does return array not return a list of arrays or does the Next method refine it to one number? Pardon my ignorance.

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

                You have to put parantheses on the constructor, i.e. use "new Random()" instead of just "new Random". It's better to create the Random object once, and use that throughout the program, than to create a new one for every random number. When you create a new object, it will be seeded from the system clock, so if you create them too closely in time, the randomness will be poor. Also it's a waste of resources to create more than one Random object.

                --- b { font-weight: normal; }

                1 Reply Last reply
                0
                • B Belfast Child

                  Cheers Guys. Figured it out. Here's a test on a click button for anyone else interested public int GetRandomInt(int[] array, int length) { Random ranNum = new Random(); return array[ranNum.Next(0, length)]; } private void button1_Click(object sender, EventArgs e) { int [] intArray; intArray = new int[3] {123, 321, 666}; int randumNum = GetRandomInt(intArray, 3); randomLabel.Text = (randumNum.ToString()); }

                  S Offline
                  S Offline
                  Stefan Troschuetz
                  wrote on last edited by
                  #8

                  You should use ranNum.Next(length) instead of ranNum.Next(0, length) as it produces random numbers within the same range and is almost 2 times faster. Also I would suggest to retrieve the array length dynamically instead of using a fix value. Either do it when you call the GetRandomInt method or even remove the length parameter and do it inside the method.

                  int randumNum = GetRandomInt(intArray, intArray.Length);


                  "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                  www.troschuetz.de

                  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