答:api
int[] intArr=new int[100]; ArrayList myList=new ArrayList(); Random rnd=new Random(); while(myList.Count<100) { int num=rnd.Next(1,101); if(!myList.Contains(num)) myList.Add(num); } for(int i=0;i<100;i++) intArr[i]=(int)myList[i];
Random類表示僞隨機數生成器,它是生成知足某些隨機性統計要求的數字序列的設備。數組
基本用法:緩存
//使用隨機數填充指定字節數組的元素。 byte[] bytes = new byte[5]; rand.NextBytes(bytes); //返回非負隨機整數。 Console.Write("{0,15:N0}", rand.Next()); //返回小於指定最大值的非負隨機整數。 Console.Write("{0,8:N0}", rand.Next(101)); //返回指定範圍內的隨機整數。 Console.Write("{0,8:N0}", rand.Next(50, 101)); //返回大於或等於0.0且小於1.0的隨機浮點數。 Console.Write("{0,8:N3}", rand.NextDouble()); Console.Write("{0,8:N3}", rand.NextDouble() * 5);
演示:*dom
using System; class Program { static void Main() { //建立新的Random對象. Random random = new Random(); //獲取3個隨機數. //這些老是5,6,7,8或9. Console.WriteLine("RANDOM: " + random.Next(5, 10)); Console.WriteLine("RANDOM: " + random.Next(5, 10)); Console.WriteLine("RANDOM: " + random.Next(5, 10)); } }
加密:ide
對於簡單的任務,Random類就足夠了。但對於複雜的事物,RNGCryptoServiceProvider更好。加密
using System; class Program { static void Main() { //調用使用類級Random的方法. UseStatic(); //調用相同的方法. //隨機數序列仍然是隨機的. UseStatic(); } static Random _random = new Random(); static void UseStatic() { //使用類級隨機。 int result = _random.Next(); Console.WriteLine("STATIC RANDOM: " + result); } }
使用字段或傳遞Random做爲參數以免重複的隨機數。pwa
class Program
{code
static void Main() { Test(); Test(); Test(); } static void Test() { /建立隨機並使用它。 //這是時間相關的,因此若是屢次調用能夠重複。 var random = new System.Random(); System.Console.WriteLine("CURRENT: " + random.Next()); }
}對象
將整數傳遞給Random.Next()時,獲得的結果範圍爲0到maxValue - 1。排序
using System; class Program { static void Main() { F(); F(); F(); F(); F(); F(); } static Random _r = new Random(); static void F() { int n = _r.Next(5); //能夠返回0,1,2,3或4。. Console.WriteLine(n); } }
ToArray將語句中的可枚舉字母再次轉換爲字符串,生成最終結果:隨機字符串。
using System; using System.Linq; class Program { static void Main() { const string original = "senators"; Random num = new Random(); //從從新排序的char數組中建立新字符串。. string rand = new string(original.ToCharArray(). OrderBy(s => (num.Next(2) % 2) == 0).ToArray()); Console.WriteLine("original: {0}\r\nrand: {1}", original, rand); Console.Read(); } }
在Random類型上調用NextBytes方法以獲取隨機字節數組。每一個字節的小數範圍爲0到255。
using System; class Program { static void Main() { //將隨機字節放入此數組中. byte[] array = new byte[8]; //使用Random class和NextBytes方法。 //使用如下方法顯示字節。. Random random = new Random(); random.NextBytes(array); Display(array); random.NextBytes(array); Display(array); } static void Display(byte[] array) { //循環並顯示數組中的字節. foreach (byte value in array) { Console.Write(value); Console.Write(' '); } Console.WriteLine(); } }
使用隨機整數預先填充List。
using System; using System.Collections.Generic; using System.Diagnostics; class Program { static Random _random = new Random(); static List<int> _randomCache = new List<int>(); static void AddRandoms() { //將隨機數添加到列表中供之後使用. for (int i = 0; i < 1000; i++) { _randomCache.Add(_random.Next()); } } const int _max = 10000000; static void Main() { //預先計算一些隨機的int. AddRandoms(); //獲取新的隨機int. var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { int random = _random.Next(); if (random < 0) { return; } } s1.Stop(); //從列表中使用緩存的隨機int. var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { int random = _randomCache[i % _randomCache.Count]; if (random < 0) { return; } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); } }