主题:[原创]急!急!急!求高手帮助随机数问题
我的程序如下:但运行结果总是有重复数出现,例如:84241635、问各位高手我的哪里错了,帮指出谢谢,我在线等、
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
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(arrNum, tmp, minValue, maxValue, ra);
//arrNum[i] = getDifNum(arrNum,num,tmp, minValue, maxValue, ra);
}
return arrNum;
}
public int getNum(int[] arrNum, int tmp, int minValue, int maxValue, Random ra)//获取随机数判断是否有重复数
{
int n = 0;
string box = "";
//定义一个计数器;
while (n <= arrNum.Length - 1)
{
string box_1 = "";
if (arrNum[n] == tmp)//利用if循环判断是否有重复数
{
tmp = ra.Next(minValue, maxValue);
getNum(arrNum, tmp, minValue, maxValue, ra);
}
n++;
}
return tmp;
}
protected void Button1_Click(object sender, EventArgs e)
{
int[] arr = getRandomNum(8, 1, 9);
int i = 0;
string temp = "";
while (i <= arr.Length - 1)
{
temp+=arr[i].ToString() + "\n";
i++;
}
Label1.Text = temp;//.LastIndexOf("");
}
能不能实现8个数字在8位上分别出现,没有重复数字出现。能么 ,跪求 帮助!
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
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(arrNum, tmp, minValue, maxValue, ra);
//arrNum[i] = getDifNum(arrNum,num,tmp, minValue, maxValue, ra);
}
return arrNum;
}
public int getNum(int[] arrNum, int tmp, int minValue, int maxValue, Random ra)//获取随机数判断是否有重复数
{
int n = 0;
string box = "";
//定义一个计数器;
while (n <= arrNum.Length - 1)
{
string box_1 = "";
if (arrNum[n] == tmp)//利用if循环判断是否有重复数
{
tmp = ra.Next(minValue, maxValue);
getNum(arrNum, tmp, minValue, maxValue, ra);
}
n++;
}
return tmp;
}
protected void Button1_Click(object sender, EventArgs e)
{
int[] arr = getRandomNum(8, 1, 9);
int i = 0;
string temp = "";
while (i <= arr.Length - 1)
{
temp+=arr[i].ToString() + "\n";
i++;
}
Label1.Text = temp;//.LastIndexOf("");
}
能不能实现8个数字在8位上分别出现,没有重复数字出现。能么 ,跪求 帮助!