C#生成随机数
来源:互联网 发布:上海软件市场 编辑:程序博客网 时间:2024/04/30 08:19
int[] RandomNum = getRandomNum(1, 1,10); //
说明:
1、返回的数据是一个整型数组
2、简言之就是按区间生成随机数
//生成多少个数,生成最小数,生成最大数
public int[] getRandomNum(int num, int minValue, int maxValue)
{Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
int[] arrNum = new int[num];
int tmp = 0;
for (int i = 0; i <= num - 1; i++)
{
tmp = ra.Next(minValue, maxValue); //随机取数
arrNum[i] = getNum(ref arrNum, ref tmp, minValue, maxValue, ra); //取出值赋到数组中
}
return arrNum;
}
public int getNum(ref int[] arrNum, ref int tmp, int minValue, int maxValue, Random ra)
{
int n = 0;
while (n < arrNum.Length)
{
if (arrNum[n] == tmp) //利用循环判断是否有重复
{
tmp = ra.Next(minValue, maxValue); //重新随机获取。
getNum(ref arrNum, ref tmp, minValue, maxValue, ra);//递归:如果取出来的数字和已取得的数字有重复就重新随机获取。
}
n++;
}
return tmp;
}
0 0
- C#生成随机数
- C#生成随机数
- C#生成随机数
- C# 生成随机数
- C#生成随机数
- c#生成随机数
- C# 随机数生成
- C#生成随机数
- C# 生成随机数
- C#生成随机数
- C# 生成随机数
- C#随机数生成
- C# 下生成随机数
- c#生成随机数
- c#生成随机数
- c# 生成随机数
- C# 生成随机数
- C# 生成随机数问题
- skb_buf数据结构
- iOS 8 模拟器
- erlang下A*算法的效率优化过程
- ***(leetcode) (tree) Flatten Binary Tree to Linked List
- CentOS 6.5 yum安装配置lnmp服务器(Nginx+PHP+MySQL)
- C#生成随机数
- 搭建并运行基于HA模式的canal
- mysql 的一个死锁问题解决
- jquery 自带的提示样式
- 自定义控件例如LinearLayout 的三种方法
- 海淘记录第二
- SQL锁表语句
- 客户端会话成功连接至Oracle服务端后,监听的启动和关闭就不会影响已经连接的会话
- DB2 操作无效:已关闭 result set。 ERRORCODE=-4470, SQLSTATE=null