需要使用密钥进行可重复的随机数组洗牌

发布于 2024-08-22 10:14:53 字数 1410 浏览 6 评论 0原文

我希望使用键随机洗牌列表/数组。我希望能够使用密钥重复相同的随机顺序。

因此,我将随机生成一个从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮云落日 2024-08-29 10:14:53

进行随机排列,用您的密钥为 RNG 播种。

 /**
     * Randomly permutes the array of this permutation. All permutations occur with approximately equal
     * likelihood. This implementation traverses the permutation array forward, from the first element up to
     * the second last, repeatedly swapping a randomly selected element into the "current position". Elements
     * are randomly selected from the portion of the permutation array that runs from the current position to
     * the last element, inclusive.
     * <p>
     * This method runs in linear time.
     */
    public static void shuffle(Random random, int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            swap(a, i, i + random.nextInt(a.length - i));
        }
    }

Do a random permutation, seed the RNG with your key.

 /**
     * Randomly permutes the array of this permutation. All permutations occur with approximately equal
     * likelihood. This implementation traverses the permutation array forward, from the first element up to
     * the second last, repeatedly swapping a randomly selected element into the "current position". Elements
     * are randomly selected from the portion of the permutation array that runs from the current position to
     * the last element, inclusive.
     * <p>
     * This method runs in linear time.
     */
    public static void shuffle(Random random, int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            swap(a, i, i + random.nextInt(a.length - i));
        }
    }
柏拉图鍀咏恒 2024-08-29 10:14:53

实施类似 Fisher-Yates 的内容。不要自己卷。这很可能是错误的。 使用指定值为 Random 构造函数设定种子。然后随机播放将是可重复的。

或者,可以使用 linq 很好地完成此操作:

var key=0;
var r=new Random(key);
myList.OrderBy(x=>r.Next());

更改 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:

var key=0;
var r=new Random(key);
myList.OrderBy(x=>r.Next());

Change the value of key to change the shuffle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文