13,295,218 members (69,641 online)
Rate this:
See more:
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.7K
Updated 7-Jan-13 4:45am
v3

Rate this:

## 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.)

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

—SA
v2
__TR__ 7-Jan-13 11:15am

+5

Thank you,
—SA
Rate this:

## 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);
return rn;
}```

Other way could be to use Guids instead of numbers for unique values.
Marcus Kramer 7-Jan-13 9:50am

__TR__ 7-Jan-13 11:15am

+5

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:

## 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.

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.

OK, I does not justify this post at all. I can see no use of it, only the confusion.
—SA

Top Experts
Last 24hrsThis month
 _duDE 120 Dave Kreskowiak 90 Richard MacCutchan 80 Richard Deeming 70 F-ES Sitecore 70
 OriginalGriff 2,131 Jochen Arndt 1,180 Richard MacCutchan 1,120 ppolymorphe 917 Dave Kreskowiak 915