0

This is the first time I'm writing a C# code.

In my C# code, I need to generate a string that can be any of these: "00000041", "0000424E", "00004244", "00004D53"

How can you do this? How can you specify strings and randomly generate anyone from them?

kevintjuh93
  • 1,010
  • 7
  • 22
Azfar Kashif
  • 173
  • 1
  • 14
  • 1
    So you just need to randomly pick from those 4 strings? – rory.ap Oct 01 '15 at 11:47
  • If you need to randomly generate one of predefined strings - then as possible option you can store them in array and randomly generate index to pick string. – Andrey Korneyev Oct 01 '15 at 11:48
  • You will need to give the rule used for generating the strings. It is impossible to know the rule from just examples. – sdgfsdh Oct 01 '15 at 11:49

2 Answers2

3

this selects randomly one string out of the list of predefined strings

Random rnd= new Random();
List<string> validStrings= new List<string>() {
 "00000041", 
 "0000424E", 
 "00004244", 
 "00004D53" };
string result = validStrings[rnd.Next(0, validStrings.Count)];
fubo
  • 44,811
  • 17
  • 103
  • 137
1
string[] s1 = new string[4] { "00000041", "0000424E", "00004244", "00004D53" };
Random rnd = new Random();
int randIndex = rnd.Next(0,4);
var randomString = s1[randIndex];
Abhilash Augustine
  • 4,128
  • 1
  • 24
  • 24