Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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
Posted 7-Jan-13 4:38am
kingsa1.5K
Edited 7-Jan-13 4:45am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
v2
Comments
__TR__ at 7-Jan-13 11:15am
   
+5
Sergey Alexandrovich Kryukov at 7-Jan-13 11:42am
   
Thank you,
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
}
 
Other way could be to use Guids instead of numbers for unique values.
  Permalink  
Comments
Marcus Kramer at 7-Jan-13 9:50am
   
Was about to post the same basic answer. +5.
__TR__ at 7-Jan-13 11:15am
   
+5
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 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)

  Print Answers RSS
0 Zoltán Zörgő 265
1 /\jmot 205
2 CHill60 145
3 Peter Leow 145
4 Sergey Alexandrovich Kryukov 115
0 Sergey Alexandrovich Kryukov 9,353
1 OriginalGriff 6,881
2 Peter Leow 4,577
3 Zoltán Zörgő 4,129
4 Richard MacCutchan 2,882


Advertise | Privacy | Mobile
Web03 | 2.8.150129.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100