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. Web Development
  3. ASP.NET
  4. Unique Array Data

Unique Array Data

Scheduled Pinned Locked Moved ASP.NET
data-structureshelptutoriallounge
8 Posts 2 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
    munklefish
    wrote on last edited by
    #1

    Hi, Im displaying 10 images on the page. This images are created by a function which pics a random image from a directory on my site. However i want to make sure each of the 10 images displayed in unique / different from the other 9. Im not sure how to do this, so i decided to create a string array. The randPic function/method is called 10 times and the returned string is stored sequentially in the array. However i am looking for an effecient (or any) way to ensure that i dont add a string if it is already stored elsewhere in the aray. Any help would be appreciated. Thanks.

    J 1 Reply Last reply
    0
    • M munklefish

      Hi, Im displaying 10 images on the page. This images are created by a function which pics a random image from a directory on my site. However i want to make sure each of the 10 images displayed in unique / different from the other 9. Im not sure how to do this, so i decided to create a string array. The randPic function/method is called 10 times and the returned string is stored sequentially in the array. However i am looking for an effecient (or any) way to ensure that i dont add a string if it is already stored elsewhere in the aray. Any help would be appreciated. Thanks.

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      There are 2 ways to approach this. Neither are particularly hard to code 1) Pick an item at random, and check it does not already exist in the picked items. if it doesnt then add it to the picked items, if it does then pick another one. Lather, rinse repeat until you have enough items in the picked list 2) Place every item into a list and pick an item from it at random. Add this item to a selected list and remove it from the pick list. Lather rinse & repeat until you have enough items. 2) above is the more efficient way IMO, but 1) will work just fine.

      M 1 Reply Last reply
      0
      • J J4amieC

        There are 2 ways to approach this. Neither are particularly hard to code 1) Pick an item at random, and check it does not already exist in the picked items. if it doesnt then add it to the picked items, if it does then pick another one. Lather, rinse repeat until you have enough items in the picked list 2) Place every item into a list and pick an item from it at random. Add this item to a selected list and remove it from the pick list. Lather rinse & repeat until you have enough items. 2) above is the more efficient way IMO, but 1) will work just fine.

        M Offline
        M Offline
        munklefish
        wrote on last edited by
        #3

        Ok i am currently using the code below to get my random image. How could i change it to ensure i only get unique images?

        // find random image from server
        string outPut = null;
        string path = Server.MapPath("photographs/"+type);
        string[] files = Directory.GetFiles(path);
        Random r = new Random();
        FileInfo fi = new FileInfo(files[r.Next(0, files.Length);]);
        outPut = "photographs/" + type + "/" + fi.Name;
        return outPut;

        J 1 Reply Last reply
        0
        • M munklefish

          Ok i am currently using the code below to get my random image. How could i change it to ensure i only get unique images?

          // find random image from server
          string outPut = null;
          string path = Server.MapPath("photographs/"+type);
          string[] files = Directory.GetFiles(path);
          Random r = new Random();
          FileInfo fi = new FileInfo(files[r.Next(0, files.Length);]);
          outPut = "photographs/" + type + "/" + fi.Name;
          return outPut;

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          munklefish wrote:

          Ok i am currently using the code below to get my random image. How could i change it to ensure i only get unique images?

          Re-read my post above, and implement the one of your choice! Were you expecting me to code this for you?

          M 1 Reply Last reply
          0
          • J J4amieC

            munklefish wrote:

            Ok i am currently using the code below to get my random image. How could i change it to ensure i only get unique images?

            Re-read my post above, and implement the one of your choice! Were you expecting me to code this for you?

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

            I know the theory as its quite simple. However doing it requires prior knowledge of such tasks. I dont have this hence i posted here. Thats how i thought help forums worked. Normally people give some sort of help with coding and/or links to relevant info. Thanks anyway.

            J 1 Reply Last reply
            0
            • M munklefish

              I know the theory as its quite simple. However doing it requires prior knowledge of such tasks. I dont have this hence i posted here. Thats how i thought help forums worked. Normally people give some sort of help with coding and/or links to relevant info. Thanks anyway.

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              munklefish wrote:

              Normally people give some sort of help with coding and/or links to relevant info.

              hmm.. such as outlining exactly the process you need to follow to accomplish a task. Im sorry we don't hand-hold here. If you give it a try, and run into difficulties post some code and someone will surely help. However, we dont write code for you...presumably you're being paid to write this code. If you want me to write it, i'll want paying!

              M 1 Reply Last reply
              0
              • J J4amieC

                munklefish wrote:

                Normally people give some sort of help with coding and/or links to relevant info.

                hmm.. such as outlining exactly the process you need to follow to accomplish a task. Im sorry we don't hand-hold here. If you give it a try, and run into difficulties post some code and someone will surely help. However, we dont write code for you...presumably you're being paid to write this code. If you want me to write it, i'll want paying!

                M Offline
                M Offline
                munklefish
                wrote on last edited by
                #7

                Actually its a project for myself. But sorry to waste your time.

                J 1 Reply Last reply
                0
                • M munklefish

                  Actually its a project for myself. But sorry to waste your time.

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #8

                  munklefish wrote:

                  But sorry to waste your time.

                  No problem

                  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