怎样 随机选出 ..不重复的数字喃 ?
string s = String.Empty;
int len = li.Count;
Random rand = new Random(len);
for (int i = 0; i <6; i++)
{
int r = rand.Next(0,len );
temp = li[r];
s += Convert.ToString(temp) + " ";
textBox1.Text = s;
}
这样会有重复的 数字 ... 怎么让它产生不重复的数字喃?
[解决办法]
宣告一個陣列,利用隨機數產生器Random.Next(),來設計一個程式由電腦產生出1~13的數字共13個,且數字不得重複。EX:1、6、2、9、11、5、8、10、3、12、7、4、13。using System;using System.Collections.Generic;using System.Text;namespace Rnd13{class Program{static void Main(string[] args){Random rnd = new Random();//1~13取亂數 不重複int n = 13;int[] b = new int[13]; //設13個陣列//while (n != 0){int t = rnd.Next(13); //取亂數//if (b[t] != -1) //b[t]不等於-1//{Console.Write("{0} ", t + 1); //把質列出//b[t] = -1; //b[t]等於-1就不取//--n;}}}}}