初学者求救关于随机数的问题!

qq3583583 2006-10-24 05:23:51
我刚刚学习C#,很多不懂,希望大家帮帮我,谢谢!!
我要得出一组(6个)随机数字,比如,123456,可是随机函数会产生相同的数字,象122223,111111,之类的数字,我现在要的是全部都不相同的数字,也就是没有一个数字是重复的,应该怎么限制呢?用什么方法呢?
代码应该怎么写呢?在线等待ing~~~!!!
...全文
156 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
shoushii 2006-11-08
  • 打赏
  • 举报
回复
icehawk(我是一只小小鸟)
好方法
顺便问一下,不可以用正则表达式吗?
qq3583583 2006-10-24
  • 打赏
  • 举报
回复
我不是想要连续的6个数字,是分别的,: 1,2,3,4,5,6 不是6位的整数,你们能帮我再看看吗?谢谢
icehawk 2006-10-24
  • 打赏
  • 举报
回复
private Random atempRandomobj = new Random();
private int[] temp = new int[6];
private void button1_Click(object sender, EventArgs e)
{

for (int i = 0; i < 6; i++)
{
temp[i] = getrandom(i);
}
int tempvalue = temp[5] * 100000 + temp[4] * 10000 + temp[3] * 1000 + temp[2] * 100 + temp[1] * 10 + temp[0];
MessageBox.Show(tempvalue.ToString());
}

private int getrandom(int index)
{
int tempvalue = atempRandomobj.Next(1,9);
bool flag = true;
for (int j = 0; j <= index; j++)
{
if (tempvalue == temp[j])
{
flag = false;
break;
}
}
if (flag)
{
return tempvalue;
}
else
{
return getrandom(index);
}
}

bsh_ly 2006-10-24
  • 打赏
  • 举报
回复
用机函数的时候判断下
若已经存在相同的 就在一直调用 不就可以了?
huang_u 2006-10-24
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/4976/4976478.xml?temp=.4058802

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧