/*
*这个是产生任意范围内,任意个不重复的整数,函数返回值为一个数组,整数之间以空格隔开!
* 由SharpDevelop创建。
* 用户: 石书义
* 日期: 2011-5-7
* 时间: 16:38
* * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
using System.Collections;
using System.Net;
namespace CreateRandomNum {
class Program {
public static void Main(string[] args) {
Console.Write("随机数:" + CreateNum(1,100,30));
Console.ReadKey(true);
}
public static string CreateNum(int first,int last,int num) {
ArrayList MyArray = new ArrayList();
Random random = new Random();
string str = null;
//循环的次数
int Nums = num;
while (Nums > 0) {
int n = random.Next(first,last);
if (!MyArray.Contains(n)) {
if (MyArray.Count < num) {
MyArray.Add(n); Nums -= 1;
}
}
}
for (int i = 0; i <= MyArray.Count - 1; i++) {
str += MyArray[i].ToString() + " ";
}
return str;
}
}
}
C#随机数函数
于 2011-05-07 19:04:00 首次发布