【问题标题】:Need a repeatable random array shuffle using a key需要使用密钥进行可重复的随机数组洗牌
【发布时间】:2011-01-19 00:58:53
【问题描述】:

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

所以我会随机生成一个从 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];
        }
    }

【问题讨论】:

    标签: arrays random sorting key shuffle


    【解决方案1】:

    做一个随机排列,用你的密钥播种 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));
            }
        }
    

    【讨论】:

    • 感谢您的回复。如果我想重复我的数组的相同顺序,就没有办法这样做,因为 random.nextInt 每次调用都会返回不同的数字。我需要一种方法来重现相同的随机播放顺序
    • 用固定种子播种随机类。
    【解决方案2】:

    实现类似Fisher-Yates 的东西。不要自己滚。这很可能是错误的。 Seed the Random constructor 具有指定值。然后随机播放将是可重复的。

    或者,这可以用 linq 很好地完成:

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

    更改key 的值以更改随机播放。

    【讨论】:

    • 我没有使用 Linq。但这很有趣。 .OrderBy 可以采用索引而不是字段。那么它会在随机字段上按升序排序吗?
    • 嗯,这并不是真正“按字段”排序,而是评估字段的值。这不一定是 lambda 参数的属性(在上面的例子中是 x),而是范围内的任何东西。
    猜你喜欢
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 2021-09-23
    相关资源
    最近更新 更多