1

Possible Duplicate:
How can I generate random 8 character, alphanumeric strings in C#?

I have the array in the name of letters.now i need to select 5 character randomly.If i use the following code i can select only one character.How to Select the 5 digits in given array string random?

    String[] letters = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q" };
    Random rand = new Random();
    String number = letters[rand.Next(1,10)];
Community
  • 1
  • 1
V.V
  • 875
  • 3
  • 25
  • 54

3 Answers3

5
string number = 
    string.Concat(Enumerable.Range(0,5).Select(i => 
        letters[rand.Next(0, 10)]));

Of course, given your array contents, this is functionally equivalent to:

string number = rand.Next(0, 100000).ToString("D5");
Douglas
  • 53,759
  • 13
  • 140
  • 188
  • 1
    The "digits" in the question contain 0-9 and A-Q... neither of the samples shown here actually work! – Marc Gravell Nov 16 '12 at 10:44
  • The OP used `rand.Next(1,10)` in their sample code; I assume they want 0–9 only, with the rest of the array being irrelevant for the scope of this question. – Douglas Nov 16 '12 at 10:45
  • 1
    hmmm, fair point - I guess that comes down to which part of the question is correct, and which is an error (as written, the question itself is slightly contradictory) – Marc Gravell Nov 16 '12 at 10:46
  • Yes, on that I agree. Especially since it makes little sense to construct a random five-character string of 0–9 digits manually, when you can get the job much more easier done using `rand` directly. – Douglas Nov 16 '12 at 10:48
  • Although! If we assume the question is correct, then the OP is **actually** selecting "2" thru "0", not "1" thru "0". – Marc Gravell Nov 16 '12 at 10:48
  • From "2" to "A" (inclusive), I'm afraid. I assumed that was an error by the OP. – Douglas Nov 16 '12 at 10:50
  • 1
    no, the 10 is exclusive, so it is index 1-thru-9; "A" is not a possible outcome in the original code – Marc Gravell Nov 16 '12 at 10:50
  • You're right... I had mistakenly assumed the parameters to mean `Random.Next(start,count)` (like for `Enumerable.Range`), instead of `Random.Next(min,max)`. – Douglas Nov 16 '12 at 10:52
3
string[] selected = new string[5];
Random rand = new Random();
for(int i = 0 ; i < selected.Length ; i++)
{
    selected[i] = letters[rand.Next(letters.Length)];
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2
var randomSelection = (from c in letters orderby rand.Next() select c).Take(5);
Hari
  • 4,514
  • 2
  • 31
  • 33
  • 1
    Your algorithm never gives the same letter twice. This is more like *suffling* – L.B Nov 16 '12 at 10:52
  • Indeed that is the point of the answer. In the question it is not specified if multiple occurences for the same letter are allowed or not. And so far this is the only answer returning a randomly selected distinct set. – Hari Nov 16 '12 at 10:55
  • 2
    +1: Fair point. The intent of the question is ambiguous. – Douglas Nov 16 '12 at 10:56
  • Undid the downvote, althought I disagree. `order by` a random data to suffle is also not a good idea. – L.B Nov 16 '12 at 10:58
  • @L.B: Why isn't it a good approach for shuffling? – Douglas Nov 16 '12 at 11:00
  • 1
    No it is not good. http://blogs.msdn.com/b/ericlippert/archive/2011/01/31/spot-the-defect-bad-comparisons-part-four.aspx – L.B Nov 16 '12 at 11:01
  • 1
    Yes, that's true, the current implementation gives inconsistent values each time the sort key is accessed. – Douglas Nov 16 '12 at 11:04
  • 1
    @L.B I think your point is not valid. This is Linq not Sort. Link will query rand.next() for each letter only once and use that as the key for ordering. – Hari Nov 16 '12 at 11:26
  • I don't know how you name `OrderBy` but it is a sorting code. – L.B Nov 16 '12 at 11:33
  • Of course it is, I just hoped that terminology will better demonstrate what is the difference between what is the linked atticle is about and the current case, and why your point is invalid. – Hari Nov 16 '12 at 11:35
  • 1
    @L.B: Actually, @Hari is right. I checked the implementation of `Enumerable.OrderBy`; the keys are precomputed and stored in an internal array (through the `EnumerableSorter.ComputeKeys` method) _before_ the sorting commences. – Douglas Nov 16 '12 at 13:30