Unique Array Data
-
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.
-
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.
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.
-
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.
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; -
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; -
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?
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.
-
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.
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!
-
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!
Actually its a project for myself. But sorry to waste your time.
-
Actually its a project for myself. But sorry to waste your time.