为什么还能抽出1-10的随机数?
protected void Page_Load(object sender, EventArgs e)
{
int[] sj={1,2,3,4,5,6,7,8,9,10};
Random ra=new Random();
div_ss.InnerText = getNum(sj, ra.Next(1, 20)).ToString();
}
public int getNum(int[] arrNum, int tmp)
{
List<int> sum = new List<int>();
sum.AddRange(arrNum);
sum.Sort();
Random ra = new Random();
foreach (int a in sum)
{
if (a == tmp)
{
getNum(arrNum, ra.Next(1, 20));
}
}
return tmp;
}
------解决方案--------------------sorry
写错了
div_ss.InnerText = Enumerable.Range(1, 20).Except(sj).OrderBy(x => Guid.NewGuid()).First().ToString();
------解决方案--------------------static List<int> nums = new List<int>(); //写在方法外面
int x = Enumerable.Range(1, 20).Except(sj).Except(nums).OrderBy(x => Guid.NewGuid()).First();
nums.Add(x);
div_ss.InnerText = x.ToString();