首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么 随机选出 .不重复的数字喃

2012-08-27 
怎样 随机选出 ..不重复的数字喃 ?string s String.Emptyint len li.CountRandom rand new Random

怎样 随机选出 ..不重复的数字喃 ?
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;
   
  }

这样会有重复的 数字 ... 怎么让它产生不重复的数字喃?

[解决办法]

C# code
宣告一個陣列,利用隨機數產生器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;}}}}} 

热点排行