-2

I have a project, in which I want to be able to have the computer select a random assortment of items, the number of which being left up to the user.

At first I thought that I have to have an array of numbers, like 1-16 for example, and then have an array for the actual things being randomly selected, like violin, viola, etc. Then I thought, maybe I just need one array, and Random can do the rest even if it's an array of strings, or a data file or whatever.

Am I on the right track here with this part of the program? Can Random pick a random assortment of, strings or char's? The purpose of this program will be for when I want to write music, and don't care about what combination of instruments I have, and am not feeling particularly creative at the moment.

g t
  • 7,287
  • 7
  • 50
  • 85
Patrick
  • 5
  • 2
  • 3
    Store your items in a collection that has a numerical indexer, then generate a random number for the index and get the element at that index. – CodeCaster Feb 21 '17 at 08:58
  • Will "no" answer your question? Cause this is it. – MakePeaceGreatAgain Feb 21 '17 at 09:00
  • 2
    Sure, [reopen a question](http://stackoverflow.com/posts/42362726/timeline) to post exactly the same answers as were in the duplicate... – CodeCaster Feb 21 '17 at 09:05
  • @CodeCaster I disagree. The question is *Can the random class pick more than numbers?*, not 'get the the nth item from a list. – Patrick Hofman Feb 21 '17 at 09:06
  • 1
    @Patrick _"Then I thought, maybe I just need one array, and Random can do the rest even if it's an array of strings, or a data file or whatever"_, which I interpret as _"How to get random elements from a collection"_. :) – CodeCaster Feb 21 '17 at 09:06
  • @PatrickHofman: you are splitting hairs. At best, this question is too broad, and four different answers demonstrate that nicely. But in fact, this question is directly addressed by the duplicate. By reopening the question, answering it, and opening the door for other answers, you're just polluting the Stack Overflow search results. And even if you don't like that one, there are [130 others to choose from](http://stackoverflow.com/search?q=%5Bc%23%5D+pick+random+object), most of which are also directly applicable. – Peter Duniho Feb 21 '17 at 09:09
  • @PeterDuniho Okay. In my opinion it was perfectly answerable, but I guess I was wrong. Sorry for that. – Patrick Hofman Feb 21 '17 at 09:10
  • 1
    @PatrickHofman: "answerable" doesn't make the question worthy of an answer. There's a reason we have a way to close duplicate questions. If you think you have something _novel_ to add to the general discussion of picking random non-numeric items, the place to add that is in the marked duplicate. Frankly, I'm a bit surprised and disappointed that a long-time user isn't on-board with that widely-understood philosophy of the Stack Overflow community. Answering questions like this just encourages laziness on the part of the questioners. – Peter Duniho Feb 21 '17 at 09:13
  • @PeterDuniho The duplicate does not talk about strings, or anything else. The duplicate is very specific, where this question isn't. I do support the SO philosophy and you can see that by the number of (dup) close votes I make. – Patrick Hofman Feb 21 '17 at 09:15
  • A shame that the "duplicate" answer only gives an answer for choosing one item, not multiple items. – Matthew Watson Feb 21 '17 at 09:16
  • This question is perfectly clear: `I want to be able to have the computer select a random assortment of items, the number of which being left up to the use`. I see no ambiguity there. Randomly choose N items from a selection of P items (where N <= P). – Matthew Watson Feb 21 '17 at 09:17
  • Here is an actual duplicate: http://stackoverflow.com/questions/48087/select-n-random-elements-from-a-listt-in-c-sharp (The accepted answer there is actually the same as my answer here, albeit without any code.) – Matthew Watson Feb 21 '17 at 09:20
  • 1
    @PatrickHofman: _"The duplicate does not talk about strings, or anything else"_ -- it does not need to. OOP is inherently all about generalizing solutions. A string is an object, and so selecting a random object is inherently the solution to selecting a random string. If you think this question isn't specific enough, then you should also think it's too broad to be a good question. Either way, it shouldn't have been answered, and you _should_ have voted to close. – Peter Duniho Feb 21 '17 at 09:22
  • Please continue this discussion on [meta](http://meta.stackoverflow.com/q/344253/993547). – Patrick Hofman Feb 21 '17 at 09:22
  • @PeterDuniho The question *is* specific enough to be answered, but it *is* a duplicate of an existing question - only *not* the one that has been actually marked as a duplicate. (You may not think it's specific enough to be answered, but you are not the appointed arbiter of such things AFAIAA) – Matthew Watson Feb 21 '17 at 09:25
  • @MatthewWatson: _"The question is specific enough to be answered"_ -- also irrelevant. That is _can_ be answered does not mean it should be. That's why we have the "too broad" reason. For questions than _can_ be answered, but for which there are **too many** possible answers. – Peter Duniho Feb 21 '17 at 09:26
  • @PeterDuniho I am telling you that I think that this question is quite clear, and therefore can be answered. You may disagree of course, but your opinion is not worth more than anybody else's here, yes? (I guess we could go by numbers, in which case it would appear that you are outvoted...) – Matthew Watson Feb 21 '17 at 09:28
  • 1
    @MatthewWatson: _"I am telling you that I think that this question is quite clear, and therefore can be answered"_ -- you are telling me something that is neither relevant, nor something I disagree with. Again: **that a question _can_ be answered does not mean it should be**. You are confused if you think otherwise. – Peter Duniho Feb 21 '17 at 09:29
  • @PeterDuniho The obvious remark `that a question can be answered does not mean it should be` is not relevant. The important thing is: Should THIS question be answered? My opinion for this particular question is: If it doesn't already have an answer, then it SHOULD be answered. (Although it already does have an answer as I noted above). And I am very much NOT confused. However, [Patrick has opened a thread to discuss this.](http://meta.stackoverflow.com/questions/344253/should-this-question-with-a-related-duplicate-be-closed-or-not/344256#344256) – Matthew Watson Feb 21 '17 at 09:33

1 Answers1

0

You can store your items in a List<T> collection and then access the items by index.

A simple solution could be:

var random = new Random();
var items = new List<string> { "apple", "orange", "banana" };
var index = random.Next(0, items.Count - 1);
var item = items[index];

A similar approach can be used with the ElementAt extension method provided by Linq:

var random = new Random();
var items = new List<string> { "apple", "orange", "banana" };
var item = items.ElementAt(random.Next(0, items.Count - 1));
Nahuel Ianni
  • 3,177
  • 4
  • 23
  • 30