Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从多维数组中获取随机字符串_C#_C# 4.0 - Fatal编程技术网

C# 从多维数组中获取随机字符串

C# 从多维数组中获取随机字符串,c#,c#-4.0,C#,C# 4.0,下面是我的代码 我需要一个随机数组的单个字符串 string[,] array = new string[4,3] { { "a1", "b1", "c1" }, {"a2", "b2", "c2" } , { "a3", "b3", "c3" }, { "a4", "b4", "c4" } } ; //string a =??? //string b =??? //string c =??? 我需要的是a1、b1、c

下面是我的代码 我需要一个随机数组的单个字符串

 string[,] array = new string[4,3]  { { "a1", "b1", "c1" }, {"a2", "b2", "c2" } , 
                                  { "a3", "b3", "c3" }, { "a4", "b4", "c4" } } ;

//string a =???
//string b =???
//string c =???
我需要的是a1、b1、c1或a2、b2、c2等

如有任何意见,我们将不胜感激

谢谢,
Arnab

据我所知,您希望获取随机获取的行的列。
为此,只需在行索引上使用
Math.Random()
。在本例中为数组[4]。

我强烈建议使用。在这种情况下,您可以使用以下扩展方法:

private static readonly Random _generator = new Random();

public static T RandomItem<T>(this T[] array)
{
    return array[_generator.Next(array.Length)];
}
一下子:

string[] randomValues = array.RandomItem(); // { "a3", "b3", "c3" } or ... etc.

.

这将根据字符串的第二个字符对字符串进行分组

//http://stackoverflow.com/questions/3150678/using-linq-with-2d-array-select-not-found
string[,] array = new string[4,3]  { { "a1", "b1", "c1" }, {"a2", "b2", "c2" } , 
                                  { "a3", "b3", "c3" }, { "a4", "b4", "c4" } } ;

var query = from string item in array
            select item;

var groupby = query.GroupBy(x => x[1]).ToArray();

var rand = new Random();

//Dump is an extension method from LinqPad
groupby[rand.Next(groupby.Length)].Dump();
这将输出(随机):

哈哈,杀过头了,没有读到数组已经被索引“分组”了


@Jayram不,他想要一整排的2D图像阵列。这是不同的。可能是重复的感谢你的指点..我删除了:)谢谢大家,抱歉没有详细说明要求谢谢,Random rnd=new Random();int i=rnd.Next(1,4);字符串a=数组[i,0];。。。
string randomValues = string.Join(", ", array.RandomItem()); // a4, b4, c4
//http://stackoverflow.com/questions/3150678/using-linq-with-2d-array-select-not-found
string[,] array = new string[4,3]  { { "a1", "b1", "c1" }, {"a2", "b2", "c2" } , 
                                  { "a3", "b3", "c3" }, { "a4", "b4", "c4" } } ;

var query = from string item in array
            select item;

var groupby = query.GroupBy(x => x[1]).ToArray();

var rand = new Random();

//Dump is an extension method from LinqPad
groupby[rand.Next(groupby.Length)].Dump();
> a1,b1,c1

> a2,b2,c2

> a3,b3,c3

> a4,b4,c4