昨天弄了一下,结果终于弄出了随机不重复的抽奖系统。现在出现一个问题 最大数输入10 要抽8个的话 还是会出现重复的
然后还有 我想让一二三等奖都不重复 该怎么修改~ 请高手修改下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WpfApplication4
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
} private void button1_Click(object sender, RoutedEventArgs e)
{
int max, a,b;
max = int.Parse(max1.Text);
a = int.Parse(first.Text);
b = int.Parse(second.Text);
result1.Content=getRandomNum(a, max).Trim();
result2.Content = getRandomNum(b, max).Trim(); }
public int[] sort(int[] num)
{
int i, j, temp;
int n = num.Length;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++) /*注意循环的上下限*/
{
if (num[i] > num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
return num;
}
public int getNum(int[] arrNum, int tmp, int maxValue, Random ra)
{
int n = 0;
while (n <= arrNum.Length - 1)
{
if (arrNum[n] == tmp) //利用循环判断是否有重复
{
tmp = ra.Next(1, maxValue); //重新随机获取。
getNum(arrNum, tmp,maxValue, ra);//递归
}
n++;
}
return tmp;
} public string getRandomNum(int num, int maxValue)
{
Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
string strNum = "";
int[] arrNum = new int[num];
int tmp = 0; for (int i = 0; i <= num - 1; i++)
{
tmp = ra.Next(1, maxValue); //随机取数
arrNum[i] = getNum(arrNum, tmp, maxValue, ra); //取出值赋到数组中
} this.sort(arrNum); for (int i = 0; i <arrNum.Length; i++)
{ string s = Convert.ToString(arrNum[i]);
strNum = strNum + s + " ";
}
return strNum;
}
}
}
注:这个是wpf窗口的...
然后还有 我想让一二三等奖都不重复 该怎么修改~ 请高手修改下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WpfApplication4
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
} private void button1_Click(object sender, RoutedEventArgs e)
{
int max, a,b;
max = int.Parse(max1.Text);
a = int.Parse(first.Text);
b = int.Parse(second.Text);
result1.Content=getRandomNum(a, max).Trim();
result2.Content = getRandomNum(b, max).Trim(); }
public int[] sort(int[] num)
{
int i, j, temp;
int n = num.Length;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++) /*注意循环的上下限*/
{
if (num[i] > num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
return num;
}
public int getNum(int[] arrNum, int tmp, int maxValue, Random ra)
{
int n = 0;
while (n <= arrNum.Length - 1)
{
if (arrNum[n] == tmp) //利用循环判断是否有重复
{
tmp = ra.Next(1, maxValue); //重新随机获取。
getNum(arrNum, tmp,maxValue, ra);//递归
}
n++;
}
return tmp;
} public string getRandomNum(int num, int maxValue)
{
Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
string strNum = "";
int[] arrNum = new int[num];
int tmp = 0; for (int i = 0; i <= num - 1; i++)
{
tmp = ra.Next(1, maxValue); //随机取数
arrNum[i] = getNum(arrNum, tmp, maxValue, ra); //取出值赋到数组中
} this.sort(arrNum); for (int i = 0; i <arrNum.Length; i++)
{ string s = Convert.ToString(arrNum[i]);
strNum = strNum + s + " ";
}
return strNum;
}
}
}
注:这个是wpf窗口的...
public int getNum(int[] arrNum, int tmp, int maxValue, Random ra)
{
int n = 0;
while (n <= arrNum.Length - 1)
{
if (arrNum[n] == tmp) //利用循环判断是否有重复
{
tmp = ra.Next(1, maxValue); //重新随机获取。
getNum(arrNum, tmp,maxValue, ra);//递归
}
n++;
}
return tmp;
}
改成
public int getNum(int[] arrNum, int tmp, int maxValue, Random ra)
{
int n = 0;
while (n <= arrNum.Length - 1)
{
if (arrNum[n] == tmp) //利用循环判断是否有重复
{
tmp = ra.Next(1, maxValue); //重新随机获取。
return getNum(arrNum, tmp,maxValue, ra);//递归
}
n++;
}
return tmp;
}
就正常了
{}前定义Random rand = new Random(Guid.NewGuid().GetHashCode());
则
list.RemoveAt(1);
看3楼 帖子,你那结果为什么会重复??不是因为你没做重复判断,是你判断完得出的不重复数没有及时返回去!!! 所以tmp = ra.Next(1, maxValue); //重新随机获取。
[color=#00FF00]getNum(arrNum, tmp,maxValue, ra);//递归
要改成 tmp = ra.Next(1, maxValue); //重新随机获取。
return getNum(arrNum, tmp,maxValue, ra);//递归
将重新随机获取的数返回去,退出这个函数,没有这个return,你还会继续执行之前那个循环,多此一举!!并且出错!![/color]
{
tmp = ra.Next(1, maxValue); //随机取数
arrNum[i] = getNum(arrNum, tmp, maxValue, ra); //取出值赋到数组中
}
建议你把 arrNum[i] 换成 List<int>
你可以用
int tempnum = getNum(arrNum, tmp, maxValue, ra);
if(arrNum.Containt(tempnum))
{
i--;
}
else
{
arrNum.Add(tempnum);
}
主要是因为他们有 Contain 或 ContainKey 方法 帮你来完成检测是否存在
要不然你还要自己写