C#随机数函数

/* 
*这个是产生任意范围内,任意个不重复的整数,函数返回值为一个数组,整数之间以空格隔开!
* 由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;
		} 
	} 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木叶流丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值