Random Unique Numbers in C#

Associate
Joined
25 Feb 2007
Posts
905
Location
Midlands
Hi All,

I have a list of records which each have an ID. I need to add a random selection of these into an array. The size of the selection is determined by 5% of the total amount of records.

I've managed to do this by generating a random number between 1 and the total amount of records, but I'm struggling to make sure each value is unique.

Is there a better way of doing this?

Thanks,
 
You could sort the numbers into a random order and then just pull the first n of these to put into your array, or use a data structure like a HashSet, which only allows distinct values.
 
What you want is a random permutation, google around. Should be able to do this in O(n), i.e. linear time. if I remember correctly.

if you have a lot of elements and you don't really need truely random there could be be faster methods. A lot depends on how big your set (say size n) is and what size the subset will be (say size m). if M is much smaller than n (m << n ) then you could select m random entries and check for duplication, which will rarely occur with m << n. This will give an expect runtime time in m rather than n but the worst case runtime could be a lot worse but will almost never happen.
 
My current set size is around 200 records, and will always be around this size. My subset is 5% of that.

I don't think I actually need random values as you say, just so that a mix of records are chosen.

I've seen a lot of reference to O(n) but don't actually know what it means? I'll have a look at random permutation.
 
What are you doing with these IDs?

Why must they be numbers?

What do they represent?

Do they have any explicit value?

These are the questions that you should be asking. Having to implement your own "unique" generator should always be a last resort, particularly when you have the likes of Guid.NewGuid() to hand.
 
Basically he needs unique values that will represent the index values of items in a list in sharepoint.

For x number of values in an array he will do the following:

Code:
list.items[index].anotherpropertyinSharePoint["FieldName"]

In other words he needs x number of index values - x being 5% of the total number of items in a sharepoint list.

I.e. if 100 rows in SharePoint then he needs 5 unique integer values that fall in the range 0-99.

Edit: if the upper limit was only around 200 I'd probably generate 200 integers, shuffle them and then just pick the first 5. Probably not efficient but I'm no pro.
 
Last edited:
So he doesn't need a random number at all, if he just needs to take 5% of the records:

Code:
if (list.items.Any()) {
  var numberToTake = 5 / list.items.Count() * 100;
  var theFivePercentOfRecords = list.items.Take(numberToTake);
}
don't forget to add the using
Code:
using System.Linq;
 
I'm not sure if that will work. The code I posted mentioning 'list' is actually from a snippet of code where list is an SPList as defined by the SharePoint Object Model. It isn't a list as in a structure. Make sense?

Edit: This will need to ensure that a different 5 items are selected each time so I guess it has to be semi-random!
 
Last edited:
so what is "list.items"?

It is essentially an object that corresponds to a list + items in SharePoint. list.Items will contain all the items in a SharePoint list. Do you reckon using Any() will work along with SharePoint objects?

Edit: cool - never knew about Any()/Take(). Will check it out.
 
Last edited:
Thanks ringo747 for answering the questions! :D

I've tried Any()/Take() as you suggested Dj_Jestar but I'm getting errors about SPListItemCollection not having a reference to those methods.

I've added using System.Linq, so I'm not sure where I've gone wrong.

Thanks,
 
It's:

'Microsoft.SharePoint.SPListItemCollection' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'Microsoft.SharePoint.SPListItemCollection' could be found (are you missing a using directive or an assembly reference?)
 
I wasn't sure if this would work but Jestar is the pro around here! I'd suggest that you still need to get 5 totally random unique numbers instead of always the same 'first 5'.

You could possible create all your numbers, shuffle them and then do a 'take 5'. Not sure if there is any pre-existing operations for shuffling.

http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp
 
Last edited:
OP do yourself a favour and install ReSharper. :)

You need to reference System.Linq as well as add the using for it.

Next question(s): Why must they be random? Why only 5% needed for this task?
 
OP do yourself a favour and install ReSharper. :)

You need to reference System.Linq as well as add the using for it.

Next question(s): Why must they be random? Why only 5% needed for this task?

I'll have a look at ReSharper.

How do I reference System.Linq? Not only is this my first Sharepoint app, it's my first C#/.Net app too!! (can you tell? :D)

Basically the background to this is that I have a list of staff (in Sharepoint) which someone has to survey each month. They have to survey 5% of staff (obviously they don't want to survey the same people each month) and I want to store the results in Sharepoint.
 
Back
Top Bottom