Click here to Skip to main content
15,886,074 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I have function:
C#
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
Posted
Updated 7-Jan-13 3:45am
v3

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
 
Share this answer
 
v2
Comments
__TR__ 7-Jan-13 11:15am    
+5
Sergey Alexandrovich Kryukov 7-Jan-13 11:42am    
Thank you,
—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:
C#
List<int> generatedNos = new List<int>;
public int Next()
{
   int rn;
   do 
   {  
     rn = Random.Next() 
   } while generatedNos.Contains(rn);
   generatedNos.Add(rn);
   return rn;
}


Other way could be to use Guids instead of numbers for unique values.
 
Share this answer
 
Comments
fjdiewornncalwe 7-Jan-13 9:50am    
Was about to post the same basic answer. +5.
__TR__ 7-Jan-13 11:15am    
+5
Sergey Alexandrovich Kryukov 7-Jan-13 11:44am    
Well, using the list is quite inadequate here; this is complexity O(N), really bad. Should be a HashMap: no more difficult then the list, but O(1) — the only acceptable time complexity.
—SA
C#
rand.Next(start,end)
generates a random number between start and end values. Now lets suppose your datatable has 3 rows and look at your code:
C#
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
}


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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Jan-13 10:03am    
Isn't it obvious that this is not a solution at all? Do I even need to explain why?

You show something which does not work in principle and than asking for the purpose... And then telling "It's up to you to generate a random number". Why are you posting at all, in this case? Well...
—SA
Zafar Sultan 7-Jan-13 10:10am    
This isn't a solution. This is something I am also aware of. The reason for posting it here is the fact that people does only look at the solutions provided to them. The moment they see there is a solution and there is a comment they tend to ignore comments and restrict to the solution given to them. My motive was to explain to the OP what's going wrong. In the end I also wrote "If this unique number has anything to do with database you might have an idea about identity value." Had there been a possibility of using identity, I would have updated my answer. Like I have done in the past.
Sergey Alexandrovich Kryukov 7-Jan-13 11:45am    
OK, I does not justify this post at all. I can see no use of it, only the confusion.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900