随机数生成每次不重复

GaryJay 2010-04-07 04:01:56
怎样每次生成的随机数都和之前出现过的不一样啊
(一次一次生成)
...全文
349 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuyileneal 2010-04-08
  • 打赏
  • 举报
回复

List<Int32> list = new List<Int32>();
while(true)
{
Random r = new Random();
int n = r.Next(1,70);
if(!list.Contains(n))
{
list.Add(n);
}
if (list.Count > 20)
{
break;
}

}
//这个是输出20个1-70的不重复的数啊,没有重复的。。。
porschev 2010-04-08
  • 打赏
  • 举报
回复
先随机。。然后存在list中

下一次随机时。。。判断list是否Contains新随机出来的数字。。

如果为false。。添加到list中。。反之再随机。。。
鸭梨山大帝 2010-04-08
  • 打赏
  • 举报
回复
5楼我给出了代码,你就不愿试一试?

[Quote=引用 16 楼 garyjay 的回复:]
引用 2 楼 liuyileneal 的回复:
把之前的存起来
List<Int32> list = new List<Int32>();
while(条件)
{
Random r = new Random();
int n = r.next
if(!list.Contains(n))
{
list.Add(n);
}

}



我是在1-70中取得 那最后怎样取……
[/Quote]
spmzfz 2010-04-08
  • 打赏
  • 举报
回复
List<Int32> list = new List<Int32>();
System.Random R = new Random();
Int32 temp;

//设生成36个随机数。
for (int i = 0; i < 36; i++)
{
//Again是标签。
Again:
temp = R.Next(1, 71);
if (list.Contains(temp))
{
goto Again;
}
list.Add(temp);
}

for (int i = 0; i < list.Count ; i++)
{
Console.Write(list[i] + " ");
}
GaryJay 2010-04-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 liuyileneal 的回复:]
把之前的存起来
List<Int32> list = new List<Int32>();
while(条件)
{
Random r = new Random();
int n = r.next
if(!list.Contains(n))
{
list.Add(n);
}

}
[/Quote]


我是在1-70中取得 那最后怎样取呢 条件怎样写呢 我是了几次 好像还是有重复的哦 谢谢了
GaryJay 2010-04-08
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 spmzfz 的回复:]
List<Int32> list = new List<Int32>();
System.Random R = new Random();
Int32 temp;

//设生成36个随机数。
for (int i = 0; i < 36; i++)
{
//Again是标签。
Again:
temp = R.Next(1, 71);
if (li……
[/Quote]



大哥 不好意思 你的对我 也有用 但是 我开始没看见哦 谢谢了啊 ~~~
tuzibai 2010-04-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 liuyileneal 的回复:]
把之前的存起来
List<Int32> list = new List<Int32>();
while(条件)
{
Random r = new Random();
int n = r.next
if(!list.Contains(n))
{
list.Add(n);
}

}
[/Quote]

方便快捷
daxiao474 2010-04-07
  • 打赏
  • 举报
回复
很好 谢谢 学习中
nsq0205 2010-04-07
  • 打赏
  • 举报
回复
你的意思是要随机产生的数有相同的可能,那你测试产生的数不一样也正常呀!因为它一样的概率是很低的呀!
sagittarius168 2010-04-07
  • 打赏
  • 举报
回复
上楼的怎么输出不了?
wuyq11 2010-04-07
  • 打赏
  • 举报
回复
Random r = new Random(unchecked((int)DateTime.Now.Ticks));
古今多少事 2010-04-07
  • 打赏
  • 举报
回复
待抽取数字存到集合里,随机抽取一个,并删除这个元素。重复执行。
keeya0416 2010-04-07
  • 打赏
  • 举报
回复
利用HashSet的特性
生成数往里加 要生成多少就加到 hashset 的 size 到多少
meceky 2010-04-07
  • 打赏
  • 举报
回复
用集合或者字典
hugang001 2010-04-07
  • 打赏
  • 举报
回复
public static string RndNum1(int VcodeNum)
{
string Vchar = "0,1,2,3,4,5,6,7,8,9";

string[] VcArray = Vchar.Split(',');
string VNum = "";//由于字符串很短,就不用StringBuilder了
//int temp = -1;//记录上次随机数值,尽量避免生产几个一样的随机数

//采用一个简单的算法以保证生成随机数的不同
Random rand = new Random();
for (int i = 1; i < VcodeNum + 1; i++)
{
rand = new Random(i * unchecked((int)DateTime.Now.Ticks));
int t = rand.Next(9);
VNum += VcArray[t];
}
return VNum;
}
里面有个安日期来验证随即数的操作
rand = new Random(i * unchecked((int)DateTime.Now.Ticks));
GaryJay 2010-04-07
  • 打赏
  • 举报
回复
最好写上产生 和 取出 每次 一个哦 谢谢了
鸭梨山大帝 2010-04-07
  • 打赏
  • 举报
回复
生成之后循环判断一次,如果是之前生成过的就再次生成.


using System;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Collections.Generic;


namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
int Count = 0;
List<int> r = new List<int>();
List<int> Result = new List<int>();
int MaxCount = 50;
while (true)
{
int i = new Random().Next(1, 1000);
if (!r.Contains(i))
{
r.Add(i);
Count++;
}
if (Count >= MaxCount) break;
}
Result.AddRange(r.OrderBy(i => i));
Console.ReadKey();
}
}
}

  • 打赏
  • 举报
回复
static Random r = new Random();
yuan1238 2010-04-07
  • 打赏
  • 举报
回复
只能把先前生成的都保存起来,然后之后生成的都判断一次,只要在先前生成的那些数字里面找到了,就重新生成,否则存到之前生成的里面去!
liuyileneal 2010-04-07
  • 打赏
  • 举报
回复
把之前的存起来
List<Int32> list = new List<Int32>();
while(条件)
{
Random r = new Random();
int n = r.next
if(!list.Contains(n))
{
list.Add(n);
}

}
加载更多回复(1)

110,534

社区成员

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

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

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