对字符串数组生成一个随机序

这种情况:
一组照片,或者MP3歌曲,知道它们的路径,保存在一个数组中。现在想随机播放之。要求,每首歌出现的概率相等,且播放完全部之前不会重复,并且保证顺序可以前后无限快进或者快倒。
[code=’c#’]
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace RandomStringArray
{
class Program
{
private static int count = 50;

static void Main(string[] args)
{
string[] originalArray = GetStringArray();
string[] randomArray = new string[originalArray.Length];

SortedList list = new SortedList();
for (int i = 0; i < originalArray.Length; i++) { list.Add(originalArray[i].GetHashCode(), originalArray[i]); } int index = 0; foreach (KeyValuePair item in list)
{
randomArray[index++] = item.Value;
}

// Print Result
Console.WriteLine(“Original:”);
for (int i = 0; i < originalArray.Length; i++) { Console.WriteLine(string.Format("[{0}]{1}", i, originalArray[i])); } Console.WriteLine("Random:"); for (int i = 0; i < randomArray.Length; i++) { Console.WriteLine(string.Format("[{0}]{1}", i, randomArray[i])); } } ///

/// Generate a directories list for test
///

/// Directories list
private static string[] GetStringArray()
{
if (!Directory.Exists(“Temp”))
{
Directory.CreateDirectory(“Temp”);

for (int i = 0; i < count; i++) { Directory.CreateDirectory("Temp\\" + Guid.NewGuid().ToString()); } } return Directory.GetDirectories("Temp"); } } } [/code]

推荐文章

发表评论