随机排序

原来取随机不重复数的时候是这么写的

            int[] arr = new int[5];
            Random ran = new Random();
            for (int i = 0; i < arr.Length; )
            {
                int item = ran.Next(0,20);
                if (!arr.Contains(item))
                {
                    arr[i] = item;
                    i++;
                }
            }

            foreach (var a in arr)
            {
                Console.WriteLine(a);
            }

这样能得到不重复的随机数组

但在应用中发现,比如一个数组为{a,b,c,d,e},取索引值随机排序,random取值为0到4也就是Next(0,5)

一旦排到还剩下一个索引的时候比如还有4没有排进去,那么random还会不停的随机可能是,比如随机出来

1,2,3,0,2,3,4,这样1,2,3,0,2,3那些次都白费了,其实你知道只有一个4了

这样上面那个纯的随机数就白白耽误很多工夫,

所以后来写成这样

            List<string> list = new List<string>() { "a", "b", "c", "d", "e"};//目标数组
            List<int> indexList = new List<int>() { 0, 1, 2, 3, 4};//索引序列

            Random ran = new Random();

            for (int i = indexList.Count - 1; i >= 0; i--)
            {
                int index = ran.Next(0, indexList.Count);
                Console.WriteLine(list[indexList[index]]);//用随机数取索引值
                indexList.RemoveAt(index);//取完的索引出序列保证不重复
            }

先用一个list装进要用数组的所有索引,用随机数取索引数组的随机值,然后用List的RemoveAt函数删除已经调用过的索引,

这样就能保证不会像上面那样明明还剩下一个数,还要再多random好几次,但问题是看起来好像还是太麻烦,网上搜了搜

还有一种直接交换数组值的办法,看起来更简单些

            string[] strArray = "a,b,c,d,e".Split(‘,‘);
            Random ran = new Random();
            //随机交换数组内容,达到随机排序效果
            for (int i = 0; i < strArray.Length; i++)
            {
                int index = ran.Next(0, strArray.Length);
                string temp = strArray[i];
                strArray[i] = strArray[index];
                strArray[index] = temp;
            }

感觉这是一种通用点的方法,还有一种针对DataTable的方法,大概思路就是,在DataTable中添加一个顺序列比如叫sort

大概是这样

            DataTable dt = new DataTable();
            Random ran = new Random();
            dt.Columns.Add("sort",typeof(int));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["sort"] = ran.Next(0,100);
            }

            DataView dv = dt.AsDataView();
            dv.Sort = "sort asc";
            dt = dv.ToTable();

随机值和索引脱离开,也不怕重复。当然desc也行,如果觉得还不够随机,那可以吧next最大值写大点。

应该还有更好的方法,楼主脑残,只想到这么多

时间: 2024-08-03 17:42:23

随机排序的相关文章

java Map排序(升序、降序、随机排序)

基础知识: 1 HashMap会使用key,根据hashcode进行默认排序. 2  LinkedHashMap根据存入先后进行排序 代码展示: 1 随机排序 java Map排序(升序.降序.随机排序),布布扣,bubuko.com

随机获取数组元素 和 数组元素随机排序

<script type="text/javascript"> //随机取得数组中的一个元素 function Rand(){ var arr=[11,22,33,44]; var a=Math.floor(Math.random()*arr.length);//Math.floor(Math.random()); //Math.floor()方法执行的是向下取整计算,它返回的是小于或等于函数参数,并且与之最接近的整数. //alert(Math.random()*arr.

随机函数Math.random()_随机数,随机排序

Math.random() 返回0到1之间的随机数(小数) 如:0.6417997585228452 通过Math.random()和sort函数可实现数组的随机排序,代码如下: 1 arr.sort(function( a, b ){ 2 return Math.random() - 0.5; 3 }); 4 5 alert( arr ); //8,7,4,3,2,1,5,6 总结产生随机数的公式: x ~ y 产生x到y之间的随机整数Math.round( Math.random()*(y-

C# List 随机排序

1.List随机排序方法 public List<T> RandomSortList<T>(List<T> ListT) { Random random = new Random(); List<T> newList = new List<T>(); foreach (T item in ListT) { newList.Insert(random.Next(newList.Count + 1), item); } return newList;

Atitit.并发测试解决方案(2) -----获取随机数据库记录 随机抽取数据 随机排序 原理and实现

Atitit.并发测试解决方案(2) -----获取随机数据库记录 随机抽取数据 随机排序 1. 应用场景 1 2. 随机抽取数据原理 1 3. 常用的实现方法:::数据库随机函数 1 4. Mssql 的实现 NEWID() 跟rand()  1 5. newid()与rand()的区别 2 6. NEWID() 2 7. 参考 2 1. 应用场景 并发测试 2. 随机抽取数据原理 原理是 循环所有的ID/记录,附加随机函数字段,然后排序as 这个字段.. 3. 常用的实现方法:::数据库随机

随机排序整个数组

<script type="text/javascript"> //随机排序整个数组 var Arr1=[1,2,3,4,5,6,7,8,9,10,22,33,55,77,88,99]; Arr1.sort(function(){return Math.random()>0.5?-1:1;}); alert(Arr1); </script>

List 随机排序

List<T> l = new List<T>(); l = l.Select(a => new { a, newID = Guid.NewGuid() }).OrderBy(b => b.newID).Select(c=>c.a).ToList(); List<string> iList = new List<string>(); iList = iList.Select(a => new { a, newID = Guid.New

【shell】shuf命令,随机排序

shuf命令主要用来对输入的每一行进行随机排序输出,我们可以利用这个属性,实现在几个文件中随机读取一个的功能 如下,zls.txt文件有三行,我们想要随机从中读取一行. 可以看到,每次读取顺序都不一样 所以,我们可以根据这个属性,实现每次随机读取一行

随机排序和从大到小排序以及从小到大排序

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title></title> 6 <style type="text/css"> 7 *{ 8 margin: 0; 9 padding: 0; 10 } 11 li{list-style: none;} 12 #box{ 13 width: 500px;