随机取20个无重复数

newma 2008-06-19 12:17:30
我想在(20-46)这27个数中随机生成27个数,要求无重复.

random myRan=new random();
Hashtable hashtable = new Hashtable();
for (int i = 20; i <= 46; i++)
{
int temp = myRan.Next(20, 46);
if (!hashtable.ContainsValue(temp))
{
hashtable.Add(temp, temp);
string s = table.Rows[temp][0].ToString();
param = param + s + ",";
}
}

大家看看这样行不行,会不会影响效率。
...全文
186 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
gomoku 2008-06-19
  • 打赏
  • 举报
回复
这是个稳定的O(n)方法:


int[] nums = new int[27];
for(int i=0; i<nums.Length; i++) nums[i] = 20 + i;

Random rand = new Random();
for(int i=nums.Length-1; i > 0; i--)
{
// randomly choose a position to pop out, j belongs to [0,i-1]
int j = rand.Next( i );

// swap the popped-out to the rear the array
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
fancystyle 2008-06-19
  • 打赏
  • 举报
回复
错了,当我没说。。。。
fuadam 2008-06-19
  • 打赏
  • 举报
回复
有重复不会吧,要不你每次都new下random
fancystyle 2008-06-19
  • 打赏
  • 举报
回复


Random rand = new Random();
var randomSeq = Enumerable.Repeat(0,20).Select(i => rand.Next(20,46));
ericzhangbo1982111 2008-06-19
  • 打赏
  • 举报
回复
List<int> list = new List<int>();
random myRan=new random();
for(;;)
{
if(list.count>=28)
break;
else
{
int temp = myRan.Next(20, 46);

if(!list.Contains(temp))
list.Add(temp);
}
}
我姓区不姓区 2008-06-19
  • 打赏
  • 举报
回复

Random r = new Random();
List<int> list = new List<int>();
while(list.Count<28)
{
int i = r.Next(20,46);
if(!list.Contains(i))
list.Add(i);
}
lw065 2008-06-19
  • 打赏
  • 举报
回复
你这个方法很有可能无法生成27个数吧,for 循环才27次,除非每次都是不重复的
newma 2008-06-19
  • 打赏
  • 举报
回复
这样不行。还是有重复。
谁有更好的办法帮个忙。。。。。。。。。急。。。。。。。。。。。。。。。。。
BIGBIRDINWOODS 2008-06-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ojlovecd 的回复:]
C# code
Random r = new Random();
List<int> list = new List<int>();
while(list.Count<28)
{
int i = r.Next(20,46);
if(!list.Contains(i))
list.Add(i);
}
[/Quote]
s330481 2008-06-19
  • 打赏
  • 举报
回复
呃。。。。才发现前面已经有相似版本了
s330481 2008-06-19
  • 打赏
  • 举报
回复


static void Main(string[] args)
{
int[] a = new int[] { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46 };

Random r = new Random();
for (int i = a.Length - 1; i > 6;i-- )
{
int temp = r.Next(0, i);
int delete = a[temp];
a[temp] = a[i];
a[i] = delete;
}
for (int i = 0; i < 20; i++)
Console.WriteLine(a[i]);
Console.Read();
}

s330481 2008-06-19
  • 打赏
  • 举报
回复


static void Main(string[] args)
{
int[] a = new int[] { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46 };

Random r = new Random();
for (int i = a.Length - 1,j=0; i > 6;i--,j++ )
{
int temp = r.Next(0, i);
int delete = a[temp];
a[temp] = a[i];
a[i] = delete;
}
for (int i = 0; i < 20; i++)
Console.WriteLine(a[i]);
Console.Read();
}

yagebu1983 2008-06-19
  • 打赏
  • 举报
回复
int i = r.Next(20,46);
注意随机范围!!
yagebu1983 2008-06-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ojlovecd 的回复:]
C# code
Random r = new Random();
List<int> list = new List<int>();
while(list.Count<28)
{
int i = r.Next(20,46);
if(!list.Contains(i))
list.Add(i);
}
[/Quote]
同意!!
西安风影 2008-06-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 gomoku 的回复:]
这是个稳定的O(n)方法:

C# codeint[] nums=newint[27];for(inti=0; i<nums.Length; i++) nums[i]=20+i;

Random rand=newRandom();for(inti=nums.Length-1; i>0; i--)
{//randomly choose a position to pop out, j belongs to [0,i-1]intj=rand.Next( i );//swap the popped-out to the rear the arrayinttmp=nums[i];
nums[i]=nums[j];
nums[j]=tmp;
}
[/Quote]
非常可行
s32702 2008-06-19
  • 打赏
  • 举报
回复
3,4楼的都OK。。。。。。
aioria0815 2008-06-19
  • 打赏
  • 举报
回复
嗯...收了...............................
lw065 2008-06-19
  • 打赏
  • 举报
回复
int [] a ={20,21,22,23,24,25,26,27,28,29,30};
List<int> tmp = new List<int>(a);
List<int> result = new List<int>();
Random r = new Random();
for (int i = tmp.Count-1; i >= 0; i--)
{
int j= r.Next(tmp.Count);
result.Add(tmp[j]);
tmp.RemoveAt(j);
}

110,534

社区成员

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

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

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