关于Random 和 List<int>的Exist的方法使用
2017-06-16 00:13
381 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace List { class Program { static void Main(string[] args) { var list = Getint(); foreach (var item in list) { Console.WriteLine(item.ToString()); } Console.ReadKey(); } static Random rd = new Random(); public static List<int> Getint() { List<int> list = new List<int>(); while (true) { // Random rd = new Random(); int id =rd.Next(0, 1111); if (list.Count != 0) { if (!list.Contains(id)) { list.Add(id); }
} else { list.Add(id); } if (list.Count>3) { break; } } return list; } } }
以上的代码很简单。
两个Random放在不同的位置,产生的不同的随机值相等几率是不同的。在循环外头的生成的相同概率是比较小的。
这里的问题在于它是一个伪随机,并非一个真随机数。
随机数的选择是从随机种子开始的,默认的随机种子很容易生成相同的随机数。
当循环的速度快时,每次都去new了一个相同的随机值的概率是很大的。
之前认为,每new一个,这样产生的随机值应该是不一样的,但是由于伪随机的原因,结果一堆一样的随机值出现了。
还有那个Exists的用法也是很奇怪了,因为我只是int类型,并未使用复杂的结构体。尝试过多个写法,但是未能实现功能。最后还是只能用Contains
if (list.Exists(x => x != id))
{
list.Add(id);
}//不行
using System; using System.Collections.Generic; public class Example { public static void Main() { List<string> dinosaurs = new List<string>(); dinosaurs.Add("Compsognathus"); dinosaurs.Add("Amargasaurus"); dinosaurs.Add("Oviraptor"); dinosaurs.Add("Velociraptor"); dinosaurs.Add("Deinonychus"); dinosaurs.Add("Dilophosaurus"); dinosaurs.Add("Gallimimus"); dinosaurs.Add("Triceratops"); Console.WriteLine(); foreach(string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } Console.WriteLine("\nTrueForAll(EndsWithSaurus): {0}", dinosaurs.TrueForAll(EndsWithSaurus)); Console.WriteLine("\nFind(EndsWithSaurus): {0}", dinosaurs.Find(EndsWithSaurus)); Console.WriteLine("\nFindLast(EndsWithSaurus): {0}", dinosaurs.FindLast(EndsWithSaurus)); Console.WriteLine("\nFindAll(EndsWithSaurus):"); List<string> sublist = dinosaurs.FindAll(EndsWithSaurus); foreach(string dinosaur in sublist) { Console.WriteLine(dinosaur); } Console.WriteLine( "\n{0} elements removed by RemoveAll(EndsWithSaurus).", dinosaurs.RemoveAll(EndsWithSaurus)); Console.WriteLine("\nList now contains:"); foreach(string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } Console.WriteLine("\nExists(EndsWithSaurus): {0}", dinosaurs.Exists(EndsWithSaurus)); //改写
Console.WriteLine("\nExists(EndsWithSaurus): {0}",
dinosaurs.Exists((s) =>{ return s.ToLower().EndsWith("saurus"); }));
} // Search predicate returns true if a string ends in "saurus". private static bool EndsWithSaurus(String s) { return s.ToLower().EndsWith("saurus"); } }
循环遍历到真就返回了。
相关文章推荐
- ASP.NET List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- [C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- [C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- [C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- 【转载】[C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- [C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- 在C#.NET中,关于<%%>使用方法
- List<T>的ForEach方法(可以使用“语句lambda”作为其参数),如下例子:
- Java中使用hql,sql查询返回的list<Object> 转成需要的实体对象--方法讲解!
- jquery 中$.post获取MVC Controller中JsonResult返回包含LIst<Model>类型的子List<Model>的高级使用方法
- jquery 中$.post获取MVC Controller中JsonResult返回包含LIst<Model>类型的子List<Model>的高级使用方法
- 关于List<T> :Distinct 和 OrderBy 、GroupBy使用简例
- 解决 ”不允许在查询中显式构造实体类型“问题及使用其他方法实现返回 List<Model对象>或者IQueryable<Model对象>对象
- VS2010使用扩展方法对List<T>进行随机排序
- 关于spring切面使用<aop:aspectj-autoproxy/>报错的解决方法
- 关于<asp:checkBoxList>控件的对齐方法
- C#List<Category>使用方法
- List<T> 的条件筛选 where使用方法
- JS 使用List<Object> 对象中的Object对象的某个属性的临时处理方法