新手问题 C#求一个随机生成不重复4位数的方法

y8059904 2008-10-13 05:26:34
求教- -| 请把代码和注释写清楚…
...全文
1429 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
birdlonger 2008-10-14
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ojlovecd 的回复:]
C# code
string str = string.Empty;//定义一个string变量作为最终结果
while (str.Length < 4)//如果str的长度小于4就一直循环
{
Random r = new Random();//声明一个Random对象r
int i = r.Next(0, 10);//随机取0-9的某一个数字
if (!str.Contains(i.ToString()))//如果str中没有该数字
str += i.ToString()…
[/Quote]
mark !
dkxzq 2008-10-13
  • 打赏
  • 举报
回复
抱歉 StrDup(10,'0') 中的10换成4 我以前取的是10位
dkxzq 2008-10-13
  • 打赏
  • 举报
回复
如果是系统使用时间比较长的话 随机4位数很容易被重复的 建议把随机数长度扩大

其实一次Random就可以做到 不需要循环4次的
Random r = new Random();
r.Next(0,9999).ToString(StrDup(10,'0'))

private string StrDup(int count,char c)
{
return new string(c,count);
}


如果怕重复使用System.DateTime.Now 取年月日时分秒毫秒 再加一个3为的随机数 一般来说 绝对不会重复的
以前为保证每次取唯一还有做法是每天生成一段不重复的随机数放入数据库 然后取一个删一个 删完以后自动再生成一批
diffmaker 2008-10-13
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 XiaoMiTang 的回复:]
C# code
List<int> list = new List<int>();//声明一个集合,用来装生成的随即数

Random r = new Random();
while (true)
{
int n = r.Next(1000, 9999);//生成随即数
if (!list.Contains(n)) //不重复的添加到集合
{
list.Add(n);
}

if (list.Count == 4…
[/Quote]

10楼正解!!!!!!!!!!
flylovejings 2008-10-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ojlovecd 的回复:]
C# code
string str = string.Empty;
while (str.Length < 4)
{
Random r = new Random();
int i = r.Next(0, 10);
if (!str.Contains(i.ToString()))
str += i.ToString();
}
Console.WriteLine(str);
[/Quote]
mark!
dlmeijianyu 2008-10-13
  • 打赏
  • 举报
回复
我写了一个好懂得。这个是很笨。也许好理解些。
int a=0;
int b=0;
int c=0;
int d=0;
int sum=0; //上面变量用来存放生成的数据。
Random R = new Random();
a=R.Next(1,9); //生成了第一个数据。
do
{
b=R.Next(0,9);
}while (a==b); //这个Do循环生成第二个数据。(如果a等于b,重新生成b,直到a不等于b)
do
{
c=R.Next(0,9);
}while((a==c) ||(b==c)); //这个Do循环生成第三个数。(如果c和a或b中的某一个相等,则重新生成c)。
do
{
d=R.Next(0,9);
}while((a==d)||(b==d)||(c==d));//这个Do循环生成第四个数。(如果d和a或b或c中的某一个相等,则重新生成d)。
sum=d+10*c+100*b+1000*a; //生成四位数。
Console.WriteLine(sum.ToString());//输出。
Console.ReadLine();
舞台中央的我 2008-10-13
  • 打赏
  • 举报
回复
var a=parseInt(Math.random()*9);
var b=parseInt(Math.random()*9);
var c=parseInt(Math.random()*9);
var d=parseInt(Math.random()*9);
var e=""+a+b+c+d;
document.write(e);
cja03 2008-10-13
  • 打赏
  • 举报
回复
楼主的标题不是“随机生成不重复4位数的方法”吗????
whtydn 2008-10-13
  • 打赏
  • 举报
回复
不过方法都不好.用了数组,如果多用户 一起用..就出现问题了
lovehongyun 2008-10-13
  • 打赏
  • 举报
回复
已解!
whtydn 2008-10-13
  • 打赏
  • 举报
回复
楼上都是高人
cja03 2008-10-13
  • 打赏
  • 举报
回复
如果随机拿到的都是之前拿过去,那岂不是不停地循环?


string getValue()
{
//4位数,每一位上的数总是0-9。4个数都不能重复,那我们就从10个数中抽出这个四数(注意每抽走一个,10个数中就会少一个,这样就可以保证得到4个不重复的数,也只抽4次,不会多)。

//初始化10个数
List<int> data=new List<int>();
for(int i=0; i<10; i++)
data.Add(i);

//由于指定4位数,那么千位上就不能是0,所以先抽1-9
Random rand=new Random();
int n = rand.Next(1, 10).ToString();//从1-9中抽出一个数
data.Remove(n);//可抽数中就少了这个数(这样下次就不会抽到这个数了)
string value = n.ToString();

//同样的方法,抽3次。
for (int i = 0; i < 3; i++)
{
int n = rand.Next(data.Count);
data.Remove(n);

value += n;
}

return value;//这个就是4个不重复的四位数
}
愚痴鱼 2008-10-13
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ojlovecd 的回复:]
C# code
string str = string.Empty;//定义一个string变量作为最终结果
while (str.Length < 4)//如果str的长度小于4就一直循环
{
Random r = new Random();//声明一个Random对象r
int i = r.Next(0, 10);//随机取0-9的某一个数字
if (!str.Contains(i.ToString()))//如果str中没有该数字
str += i.ToString()…
[/Quote]

4个不重复的4位数貌似楼主要这样的
killer_liqiao 2008-10-13
  • 打赏
  • 举报
回复
up
愚痴鱼 2008-10-13
  • 打赏
  • 举报
回复

List<int> list = new List<int>();//声明一个集合,用来装生成的随即数

Random r = new Random();
while (true)
{
int n = r.Next(1000, 9999);//生成随即数
if (!list.Contains(n)) //不重复的添加到集合
{
list.Add(n);
}

if (list.Count == 4) // 够了4个就跳出
{
break;
}
}
foreach (int temp in list) //遍历,打印出来
{
Console.WriteLine(temp);
}

Console.Read();
我姓区不姓区 2008-10-13
  • 打赏
  • 举报
回复

string str = string.Empty;//定义一个string变量作为最终结果
while (str.Length < 4)//如果str的长度小于4就一直循环
{
Random r = new Random();//声明一个Random对象r
int i = r.Next(0, 10);//随机取0-9的某一个数字
if (!str.Contains(i.ToString()))//如果str中没有该数字
str += i.ToString();//则把这个数字拼接到str上
}
Console.WriteLine(str);//输出

fvflove 2008-10-13
  • 打赏
  • 举报
回复

Random newRm = new Random(); //建立一个随机
int[] Data = new int[4]; //建立一个4维数组保存数据

int i;
i = 0;
Data[i] = newRm.Next(0,9); //取得第一个随机数 (0至9之间)

int x;
while (i < 4) //循环,取够4位为止
{
Data[i]=newRm.Next(0,9);//取得下一位随机数
for(x=0;x<i;x++)// 循环判断
{
if (Data[i] == Data[x])//如果以前取得的重复了,
{
i = i - 1; //重新取随机数
}
}
i++; //不重复.取下一位
}



MessageBox.Show(Data[0].ToString()); //显示第一位
MessageBox.Show(Data[1].ToString()); //显示第二位
MessageBox.Show(Data[2].ToString()); //显示第三位
MessageBox.Show(Data[3].ToString()); //显示第四位

yijianxiangde100 2008-10-13
  • 打赏
  • 举报
回复

StringBuilder sb = new StringBuilder(50);

Random r = new Random();
int x1 = r.Next(10);
int x2 = r.Next(10);
int x3 = r.Next(10);
int x4 = r.Next(10);
if ((x1 == x2)||(x1==x3)||(x1==x4))
return;
else
sb.Append(x1.ToString() + x2.ToString() + x3.ToString() + x4.ToString());
long result=Convert.ToInt64(sb.ToString());

//小弟写的,请高手指教
fvflove 2008-10-13
  • 打赏
  • 举报
回复

Random newRm = new Random(); //建立一个随机
int[] Data = new int[4]; //建立一个4维数组保存数据

int i;
i = 0;
Data[i] = newRm.Next(0,9); //取得第一个随机数 (0至9之间)

int x;
while (i < 4) //循环,取够4位为止
{
Data[i]=newRm.Next(0,9);//取得下一位随机数
for(x=0;x<i;x++)// 循环判断
{
if (Data[i] == Data[x])//如果以前取得的重复了,
{
i = i - 1; //重新取随机数
}
}
i++; //不重复.取下一位
}



MessageBox.Show(Data[0].ToString()); //显示第一位
MessageBox.Show(Data[1].ToString()); //显示第二位
MessageBox.Show(Data[2].ToString()); //显示第三位
MessageBox.Show(Data[3].ToString()); //显示第四位

y8059904 2008-10-13
  • 打赏
  • 举报
回复
//顶一下
加载更多回复(4)

110,538

社区成员

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

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

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