//随机抽取数组中的数据
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8 };
for (int i = 0; i < 10; i++)
{
Console.WriteLine(GetRandomNumber(a));
// 按任意键,取下一个
Console.ReadKey();
}
}
// 随机抽取数组中的数据
static int GetRandomNumber(int[] a)
{
Random rnd = new Random();
int index = rnd.Next(a.Length);
return a[index];
}
}
}
//获取所有文件名,然后从数组中随机抽取
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string path = @"C:\\Users\\Administrator\\Desktop\\Music";
DirectoryInfo root = new DirectoryInfo(path);
FileInfo[] files = root.GetFiles();
for (int i = 0; i < 10; i++)
{
Console.WriteLine(GetRandomNumber(files));
// 按任意键,取下一个
Console.ReadKey();
}
}
// 随机抽取数组中的数据
static FileInfo GetRandomNumber(FileInfo[] a)
{
Random rnd = new Random();
int index = rnd.Next(a.Length);
return a[index];
}
}
}