需要使用密钥进行可重复的随机数组洗牌
我希望使用键随机洗牌列表/数组。我希望能够使用密钥重复相同的随机顺序。
因此,我将随机生成一个从 1 到 20 的数字键,然后使用该键尝试随机洗牌列表。
我首先尝试使用键继续迭代列表,递减键直到 = 0,然后抓取我所在的任何元素,删除它并将其添加到我的随机数组中。结果是随机的,但是当数组很小(我的大多数数组都是这样)和/或密钥很小时,它最终不会洗牌......似乎更像是一种转变。
我必须能够确定以下
是 csharp 中的一些示例代码的顺序:
public static TList<VoteSetupAnswer> ShuffleListWithKey(TList<VoteSetupAnswer> UnsortedList, int ShuffleKey)
{
TList<VoteSetupAnswer> SortedList = new TList<VoteSetupAnswer>();
int UnsortedListCount = UnsortedList.Count;
for (int i = 0; i < UnsortedListCount; i++)
{
int Location;
SortedList.Add(OneArrayCycle(UnsortedList, ShuffleKey, out Location));
UnsortedList.RemoveAt(Location);
}
return SortedList;
}
public static VoteSetupAnswer OneArrayCycle(TList<VoteSetupAnswer> array, int ShuffleKey, out int Location)
{
Location = 0;
if (ShuffleKey == 1)
{
Location = 0;
return array[0];
}
else
{
for (int x = 0; x <= ShuffleKey; x++)
{
if (x == ShuffleKey)
return array[Location];
Location++;
if (Location == array.Count)
Location = 0;
}
return array[Location];
}
}
I am looking to randomly shuffle a list/array using a key. I want to be able to repeat the same random order using the key.
So I will randomly generate a numeric key from say 1 to 20 then use that key to try and randomly shuffle the list.
I first tried just using the key to keep iterating through my list, decrementing the key until=0, then grabbing whatever element I am on, removing it and adding it to my shuffled array. The result is kind of random but when the arrays are small (which most of mine will be) and/or the key is small it doesn't end up shuffling... seems to be more of a shift.
I have to be able to determine what order the
Here is some sample code in csharp of :
public static TList<VoteSetupAnswer> ShuffleListWithKey(TList<VoteSetupAnswer> UnsortedList, int ShuffleKey)
{
TList<VoteSetupAnswer> SortedList = new TList<VoteSetupAnswer>();
int UnsortedListCount = UnsortedList.Count;
for (int i = 0; i < UnsortedListCount; i++)
{
int Location;
SortedList.Add(OneArrayCycle(UnsortedList, ShuffleKey, out Location));
UnsortedList.RemoveAt(Location);
}
return SortedList;
}
public static VoteSetupAnswer OneArrayCycle(TList<VoteSetupAnswer> array, int ShuffleKey, out int Location)
{
Location = 0;
if (ShuffleKey == 1)
{
Location = 0;
return array[0];
}
else
{
for (int x = 0; x <= ShuffleKey; x++)
{
if (x == ShuffleKey)
return array[Location];
Location++;
if (Location == array.Count)
Location = 0;
}
return array[Location];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
进行随机排列,用您的密钥为 RNG 播种。
Do a random permutation, seed the RNG with your key.
实施类似 Fisher-Yates 的内容。不要自己卷。这很可能是错误的。 使用指定值为 Random 构造函数设定种子。然后随机播放将是可重复的。
或者,可以使用 linq 很好地完成此操作:
更改
key
的值来更改随机播放。Implement something like Fisher-Yates. Don't roll your own. It's likely to be wrong. Seed the Random constructor with a specified value. The shuffle will then be repeatable.
Alternatively, this can be done nicely with linq:
Change the value of
key
to change the shuffle.