如下:
情况一:
一维数组:int[] Array1=new int[10]{1,2,3,4,5,6,7,8,9,10};二维数组:int[,] Array2=new int[2,10]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};或是更多维数组我想要的结果:Array1随机化:{2,5,8,3,1,4,6,7,0,9}
Array2随机化:{2,5,8,3,1,4,6,7,0,9,12,15,18,13,11,14,16,17,10,19}情况二:ArrayList[] Array1=new ArrayList();
Array1.Add(1);
Array1.Add(2);
Array1.Add(3);
Array1.Add(4);
Array1.Add(5);
Array1.Add(6);
Array1.Add(7);
Array1.Add(8);
Array1.Add(9);
Array1.Add(10);或是更数我想要的结果:Array1 链表 随机化 内容 顺序 :{2,5,8,3,1,4,6,7,0,9}或是C#.Net 中 有没有哪个类的哪个函数可以达到这种随机化效果?我可不想用Random 去将每个数的位置去随机化,而且,如果又重复的话,又要继续下一个随机,那就很麻烦了。
情况一:
一维数组:int[] Array1=new int[10]{1,2,3,4,5,6,7,8,9,10};二维数组:int[,] Array2=new int[2,10]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};或是更多维数组我想要的结果:Array1随机化:{2,5,8,3,1,4,6,7,0,9}
Array2随机化:{2,5,8,3,1,4,6,7,0,9,12,15,18,13,11,14,16,17,10,19}情况二:ArrayList[] Array1=new ArrayList();
Array1.Add(1);
Array1.Add(2);
Array1.Add(3);
Array1.Add(4);
Array1.Add(5);
Array1.Add(6);
Array1.Add(7);
Array1.Add(8);
Array1.Add(9);
Array1.Add(10);或是更数我想要的结果:Array1 链表 随机化 内容 顺序 :{2,5,8,3,1,4,6,7,0,9}或是C#.Net 中 有没有哪个类的哪个函数可以达到这种随机化效果?我可不想用Random 去将每个数的位置去随机化,而且,如果又重复的话,又要继续下一个随机,那就很麻烦了。
Random random = new Random();
for (int i = cards.Length - 1; i > 0; i--)
{
int j = random.Next(i);
int swap = cards[i];
cards[i] = cards[j];
cards[j] = swap;
}
引用自:
http://topic.csdn.net/u/20081215/14/91fde064-f80d-4493-bf5e-1a576a53744a.html
不存在重复的问题了 遍历数组 对每个元素随机一个0-9的数 然后把此元素跟这个下标的元素互换就行了
int tmp = 0;
Random rand = new Random();
for (int a = 0; a <= 10; a++)
{
int i = rand.Next(0, 10);
int j = rand.Next(0, 10);
tmp = Array1[i];
Array1[i] = Array1[j];
Array1[j] = tmp;
}
我前不久刚写了一个类似的全排列随机生成类库,随机性很好的,是应在密码学算法当中,如果你想用的话,我可以提供给你dll,代码就不提供了。直接调用就可以。.NET中肯定没有这样的算法啊
感谢2楼4楼5楼6楼朋友的算法介绍。不过我与出来的虽然不占用CPU,但要人为间隔时间去生成随机数,所以洗牌需要要时间。不知道你们写的会不会。有没有无顺序算法,又快,又有效率。因为没有人为的Thread.Sleep。Random 的DateTime.Now.Tick间隔太少,一般都是生成相同的数。
{
_isShuffling = true;
for (int i = 0; i < _theArryListCards.Count; i++)
{
int index1 = new Random().Next(i);
Thread.Sleep(5);
int index2 = new Random().Next(_theArryListCards.Count - 1);
Thread.Sleep(5);
Card tempVar = _theArryListCards[index1] as Card;
_theArryListCards[index1] = _theArryListCards[index2];
_theArryListCards[index2] = tempVar;
}
for (int i = 0; i < _theArryListCards.Count; i++)
{
_theCards[i] = _theArryListCards[i] as Card;
}
_isShuffling = false;
}
random的时间间隔小了,产生的随机数就会相同的
把Random的构造(那个new Random)放到循环外面不就可以了?
你的意思是: private void SubShuffle()
{
_isShuffling = true;
int index1 = new Random().Next(_theArryListCards.Count);
//Thread.Sleep(5);
int index2 = new Random().Next(_theArryListCards.Count);
for (int i = 0; i < _theArryListCards.Count; i++)
{ //Thread.Sleep(5);
Card tempVar = _theArryListCards[index1] as Card;
_theArryListCards[index1] = _theArryListCards[index2];
_theArryListCards[index2] = tempVar;
}
for (int i = 0; i < _theArryListCards.Count; i++)
{
_theCards[i] = _theArryListCards[i] as Card;
}
_isShuffling = false;
}什么反应都没有。
报歉,之前没有认真看您的代码思路。现在改好了,432张牌,也150ms左右搞定了,很好.
分享给你: /// <summary>
/// 交换牌
/// </summary>
private void SwapCards()
{
Random r1 = new Random();
Random r2 = new Random();
Random r3 = new Random();
int swapCount = new Random().Next(1, 3);
for (int x = 0; x < swapCount; x++)
for (int i = 0; i < _theArryListCards.Count; i++)
{
int index1 = 0;
int index2 = 0;
int randomSeed = r3.Next(2, 4);
switch (randomSeed)
{
case 2:
index1 = r1.Next(i);
index2 = r2.Next(_theArryListCards.Count);
break;
case 3:
index1 = r1.Next(i);
index2 = r2.Next(_theArryListCards.Count - 1 - i, _theArryListCards.Count);
break;
case 4:
index1 = r1.Next(_theArryListCards.Count / 2, ((_theArryListCards.Count / 2) + _theArryListCards.Count / 3));
index2 = r2.Next(((_theArryListCards.Count / 2) - _theArryListCards.Count / 3), _theArryListCards.Count / 2);
break;
default:
break;
}
Card tempVar = _theArryListCards[index1] as Card;
_theArryListCards[index1] = _theArryListCards[index2];
_theArryListCards[index2] = tempVar;
}
}