如何生成无需重复的随机数 [英] How to generate without duplication of random number

查看:153
本文介绍了如何生成无需重复的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有功能:

Hello
I have function:

public int GetRandomNumber(object dynamicDataTable)
{
   int rn = 0;
   DataTable dt = (DataTable)dynamicDataTable;
   int cnt = dt.Rows.Count + 1;
   for (int i = 0; i < dt.Rows.Count; i++)
   {
      rn = rand.Next(1, cnt);
      DataRow[] dr=dt.Select("AutoID="+rn);
      if (dr != null)
         break;
   }         
   return rn;
}



但我得到的重复值可以指导或发送任何片段到唯一值


but i am getting duplicate values can u guide or send any snippets to unique values

推荐答案

当然随机数会给你一些重复,怎么回事?为避免重复,您可以将结果存储在某个集合中,每次检查随机值是否已经存在并重复生成随机数(如果是)。



由于此搜索的性能将是一个问题,您将需要使用搜索的计算时间复杂度的算法渐近地不依赖于实际集合的大小。就你的情况而言,使用 System.Collections.Generic.HashSet< T>

http://msdn.microsoft.com/en-us/library/bb359438.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/bb356440.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/bb353005.aspx [ ^ ]。



(我还提到了你需要在简单的情况下使用的两种方法,添加和检查值。)



它会给你计算复杂 O(1)。要了解它,请参阅:

http://en.wikipedia.org/wiki/Big_O_notation [ ^ ],

http://en.wikipedia.org/wiki/Time_complexity [ ^ ]。



-SA
Of course random number will give you some duplicates, how else? To avoid duplicates, you can store the results in some collection, each time check up if the random values is already there and repeat generation of a random number if it is.

As performance of this search will be a problem, you will need to use the algorithm with the computational time complexity of the search asymptotically does not depend on the size of actual collection. Just in your case, use System.Collections.Generic.HashSet<T>:
http://msdn.microsoft.com/en-us/library/bb359438.aspx[^],
http://msdn.microsoft.com/en-us/library/bb356440.aspx[^],
http://msdn.microsoft.com/en-us/library/bb353005.aspx[^].

(I also referenced about two methods you will need to use in your simple case, to add and check up the value.)

It will give you the computational complexity of O(1). To understand it, please see also:
http://en.wikipedia.org/wiki/Big_O_notation[^],
http://en.wikipedia.org/wiki/Time_complexity[^].

—SA


其中一种方法是跟踪生成的数字。在返回下一个随机数之前,使用已生成的随机数检查新的随机数。样本:

One of the ways would be to keep track of the generated numbers. Check the new random number with the already generated ones before returning the next random. Sample:
List<int> generatedNos = new List<int>;
public int Next()
{
   int rn;
   do 
   {  
     rn = Random.Next() 
   } while generatedNos.Contains(rn);
   generatedNos.Add(rn);
   return rn;
}





其他方式可能是使用Guids而不是数字来表示唯一值。



Other way could be to use Guids instead of numbers for unique values.


rand.Next(start,end)

在起始值和结束值之间生成一个随机数。现在假设您的数据表有3行并查看您的代码:

generates a random number between start and end values. Now lets suppose your datatable has 3 rows and look at your code:

int cnt = dt.Rows.Count + 1;//cnt = 4
for (int i = 0; i < dt.Rows.Count; i++)
{
rn = rand.Next(1, cnt);//rn will have a value between 1 and 4
}





运行此代码3次,您很可能会获得重复值。现在由您决定如何生成随机数。你能告诉我们具体要求是什么吗?如果这个唯一编号与数据库有任何关系,您可能对标识值有所了解。



Running this code 3 times there is a fair possibility that you will get repeated value. Now its up to you how do you generate a random number. Can you tell us what exactly is the requirement? If this unique number has anything to do with database you might have an idea about identity value.


这篇关于如何生成无需重复的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆