数组随机排序 [英] array random sort
问题描述
如何获取数组或arraylist,并随机排序?就像假设
项目是(1,2,3,4,5)并且我想让它以随机顺序
(2,3 ,1,4,5)。你是怎样做的?我认为这是对数组或数组的调用
列出了排序方法,但我不确定你是怎么做的。
How do I take an array or arraylist, and sort it randomly? Like suppose the
items in it are (1,2,3,4,5) and I want to get it to be in a random order
(2,3,1,4,5). How do you do that? I think it''s a call to the array or array
lists sort method, but i''m not exactly sure how you do it.
推荐答案
" gl" < gl@discussions.microsoft.com>在消息中写道
news:7E ********************************** @ microsof t.com ...
"gl" <gl@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
如何获取数组或arraylist,并对其进行随机排序?就像假设
其中的项目是(1,2,3,4,5)并且我想让它以随机顺序
(2,3,1,4 ,5)。你是怎样做的?我认为这是对数组或数组的调用
列出排序方法,但我不确定你是怎么做的。
How do I take an array or arraylist, and sort it randomly? Like suppose
the
items in it are (1,2,3,4,5) and I want to get it to be in a random order
(2,3,1,4,5). How do you do that? I think it''s a call to the array or array
lists sort method, but i''m not exactly sure how you do it.
public static List< T> ; shuffledList(List< T> listToShuffle)
{
/ *
*列出从listToShuffle中挑选的元素的新列表
*随机排序。
* /
List< int> ints = new List< int>(listToShuffle.Count); // 0,1,
2,...
for(int i = 0; i< listToShuffle.Count; i ++)
ints.Add(i);
List< T> randList = new List< T>(listToShuffle.Capacity);
for(int k = 0; k< listToShuffle.Count; k ++)
{
int randIndx = Common.rand.Next(ints.Count); //随机
从0,2,..还没有被选中
int randK = ints [randIndx];
randList.Add(listToShuffle [randK]);
ints.RemoveAt(randIndx);
}
返回randList;
}
public static List<T> shuffledList(List<T> listToShuffle)
{
/*
* Make a new list of elements picked from listToShuffle
* in a random order.
*/
List<int> ints = new List<int>(listToShuffle.Count); //0, 1,
2, ...
for (int i = 0; i < listToShuffle.Count; i++)
ints.Add(i);
List<T> randList = new List<T>(listToShuffle.Capacity);
for (int k = 0; k < listToShuffle.Count; k++)
{
int randIndx = Common.rand.Next(ints.Count); //random
from 0, 2, .. not already picked
int randK = ints[randIndx];
randList.Add(listToShuffle[randK]);
ints.RemoveAt(randIndx);
}
return randList;
}
" gl" < gl@discussions.microsoft.com>在消息中写道
news:7E ********************************** @ microsof t.com ...
"gl" <gl@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
如何获取数组或arraylist,并对其进行随机排序?就像假设其中的
项目是(1,2,3,4,5)并且我想让它以随机顺序
(2,3,1,4,5)。你是怎样做的?我认为这是对数组或数组的调用
列出排序方法,但我不确定你是怎么做的。
How do I take an array or arraylist, and sort it randomly? Like suppose the
items in it are (1,2,3,4,5) and I want to get it to be in a random order
(2,3,1,4,5). How do you do that? I think it''s a call to the array or array
lists sort method, but i''m not exactly sure how you do it.
嗅探......闻闻......闻起来像是做作业。
搜索google群组进行随机播放算法
你应该找到你需要的东西
< br $>
祝你好运
比尔
Sniff....Sniff....Smells like homework.
Search google groups for shuffle algorithm
You should find what you need
Good luck
Bill
我忘了提到Common.rand是Random的一个实例,是一个
类普通的静态成员。
你可以在
http://www.frontiernet.net/~fredm/dps/Contents.htm
" Fred Mellender" <无**************** @ frontiernet.net>在消息中写道
news:3h ***************** @ news02.roc.ny ...
I forgot to mention that Common.rand is an instance of Random, and is a
static member of class Common.
You can find this, and some other useful utilities, at
http://www.frontiernet.net/~fredm/dps/Contents.htm
"Fred Mellender" <no****************@frontiernet.net> wrote in message
news:3h*****************@news02.roc.ny...
gl < gl@discussions.microsoft.com>在消息中写道
新闻:7E ********************************** @ microsof t.com。 ..
"gl" <gl@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
如何获取数组或arraylist,并随机排序?就像假设
其中的项目是(1,2,3,4,5)并且我想让它以随机顺序
(2,3,1,4 ,5)。你是怎样做的?我认为这是对数组或
数组的调用列表排序方法,但我不确定你是怎么做的。
How do I take an array or arraylist, and sort it randomly? Like suppose
the
items in it are (1,2,3,4,5) and I want to get it to be in a random order
(2,3,1,4,5). How do you do that? I think it''s a call to the array or
array
lists sort method, but i''m not exactly sure how you do it.
public静态列表< T> shuffledList(List< T> listToShuffle)
{
/ *
*以随机顺序列出从listToShuffle中挑选的元素的新列表。
* /
List< int> ints = new List< int>(listToShuffle.Count); // 0,1,
2,...
for(int i = 0; i< listToShuffle.Count; i ++)
ints.Add(i);
列表< T> randList = new List< T>(listToShuffle.Capacity);
for(int k = 0; k< listToShuffle.Count; k ++)
{
int randIndx = Common .rand.Next(ints.Count); //随机
从0,2,..还没有被选中
int randK = ints [randIndx];
randList.Add(listToShuffle [randK]);
ints.RemoveAt (randIndx);
返回randList;
}
public static List<T> shuffledList(List<T> listToShuffle)
{
/*
* Make a new list of elements picked from listToShuffle
* in a random order.
*/
List<int> ints = new List<int>(listToShuffle.Count); //0, 1,
2, ...
for (int i = 0; i < listToShuffle.Count; i++)
ints.Add(i);
List<T> randList = new List<T>(listToShuffle.Capacity);
for (int k = 0; k < listToShuffle.Count; k++)
{
int randIndx = Common.rand.Next(ints.Count); //random
from 0, 2, .. not already picked
int randK = ints[randIndx];
randList.Add(listToShuffle[randK]);
ints.RemoveAt(randIndx);
}
return randList;
}
这篇关于数组随机排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!