随机时隙算法

编程入门 行业动态 更新时间:2024-10-17 09:51:36
本文介绍了随机时隙算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有二维数组.我想随机选择一个插槽,并继续这样做,直到我最终选择了所有插槽为止,再也不要选择相同的插槽两次(因此,最后一次选择当然也不是随机的).有众所周知的算法可以做到这一点吗?我正在使用C#,但是显然,这比算法更重要,而不是任何特定平台.是的,大书"在我的购买清单上:)

I have two dimensional array. I want to pick a slot at random, and continue to do so never picking the same slot twice until I have finally picked all slots (so nothing random about the last pick of course). Is there a well known algorithm for doing this? I'm using C#, but obviously this is more about algorithms than any particular platform. Yes, 'the big book' is on my purchase list :)

推荐答案

使用 Fisher-Yates随机播放如前所述(在O(n)时间内)

Using the Fisher-Yates shuffle algorithm as mentioned before (in O(n) time)

int X = 3; int Y = 4; int[] array = new int[X * Y]; for (int i = 0; i < array.Length; i++) array[i] = i; FisherYatesShuffle(array); var randomSlots = array.Select((i,j) => new {x=array[j]%X , y=array[j]/X }) .ToArray();

public static void FisherYatesShuffle<T>(T[] array) { Random r = new Random(); for (int i = array.Length - 1; i > 0; i--) { int j = r.Next(0, i + 1); T temp = array[j]; array[j] = array[i]; array[i] = temp; } }

更多推荐

随机时隙算法

本文发布于:2023-11-27 07:44:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637160.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!