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)];
5
  • It depends. Do you want to allow the same digit more than once in the selection or you want the 5 chars to be distinct? Commented Nov 16, 2012 at 10:39
  • your current code will never select "1", btw. Or any of "A" thru "Q". There are only 9 possible outcomes, from 27 options. Commented Nov 16, 2012 at 10:49
  • It would be more efficient to have a char[] instead of string[], so that you can avoid multiple string concats for building the final string. Commented Nov 16, 2012 at 10:53
  • 1
    Or, better yet, a single string: String letters = "12345..." Commented Nov 16, 2012 at 10:54
  • I hope this isn't for a security purpose. Commented Nov 16, 2012 at 11:36

3 Answers 3

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");
Sign up to request clarification or add additional context in comments.

8 Comments

The "digits" in the question contain 0-9 and A-Q... neither of the samples shown here actually work!
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.
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)
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.
no, the 10 is exclusive, so it is index 1-thru-9; "A" is not a possible outcome in the original code
|
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)];
}

1 Comment

I'd separate the new Random() part from the rest of the code.
2
var randomSelection = (from c in letters orderby rand.Next() select c).Take(5);

11 Comments

Your algorithm never gives the same letter twice. This is more like suffling
+1: Fair point. The intent of the question is ambiguous.
Yes, that's true, the current implementation gives inconsistent values each time the sort key is accessed.
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.