总是碰到如题的问题,也许是现实世界有太多随机事件了吧。
首先取集合中一个单位作为新随机队列的初始值,然后将其余单位随机放入指定位置,当发现指定位置不为初始值时向后退一位(最后边界时回到首部)。
以下均为c#代码
下面是大饼排序问题中集合随机排序的应用片段,用于生成随机烙饼队列
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
void init()
{
//初始化赋值
cakeArray = new int[cakeNum];
cakeSizeArray = new int[cakeNum];
for (int i = 0; i < cakeSizeArray.Length; i++)
{
cakeSizeArray[i] = i;
}//大饼直径初始化
}
void randomCollection()
{
if (cakeSizeArray.Length != cakeArray.Length)
return;
Random r = new Random();
for (int i = 0; i < cakeSizeArray.Length; i++)
{
cakeArray[i] = cakeSizeArray[0];
}
for (int i = 1; i < cakeSizeArray.Length; i++)
{
int address = r.Next(10);
for(;;)
{
if (address > cakeSizeArray.Length)
address = 0;
if (cakeArray[address] != cakeSizeArray[0])
address++;
else
break;
}
cakeArray[address] = cakeSizeArray[i];
}
//打印结果
for (int i = 0; i < cakeSizeArray.Length; i++)
{
Console.Write("{0}\t", cakeSizeArray[i]);
}
Console.WriteLine();
for (int i = 0; i < cakeArray.Length; i++)
{
Console.Write("{0}\t", cakeArray[i]);
}
Console.ReadLine();
}
以下是穷举n个烙饼排列
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)