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,
 
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.
 
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?)
 
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.
 
I didn't mate, not really looked at it since. Swamped with other stuff!

I'm going to try and look at it again this week, so expect more responses in this thread!
 
Here it is mate, sorry for the delay!

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declare sites
            SPSite sourceSite = new SPSite(@"http://sharepoint");

            //Declare webs
            SPWeb sourceWeb = sourceSite.RootWeb;

            //Declare lists
            SPLists sourceList = sourceWeb.GetList("/Lists/Staff Contacts");

            SPListItemCollection sourceItems = sourceList.Items;

            try
            {
                if (sourceList.Items.Any())
                {
                    var numberToTake = 5 / sourceList.Items.Count() * 100;
                    var thePercentOfRecords = sourceList.Items.OrderBy(each => Guid.NewGuid()).Take(numberToTake);
                }

                foreach (int n in thePercentOfRecords)
                {
                    SPListItem item = sourceList.Items[n];
                    String strFirstName = item["First Name"].ToString();
                    String strLastName = item["Last Name"].ToString();
                    String strFullName = strFirstName + " " + strLastName;
                    String strTeam = item["Team"].ToString();

                    CopyItem(strFullName, strTeam);

                }

                sourceWeb.Dispose();
                sourceSite.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        static void CopyItem(String name, String team)
        {
            //Declare sites
            SPSite destSite = new SPSite(@"http://sharepoint/communications");

            //Declare webs
            SPWeb destWeb = destSite.OpenWeb();

            //Declare lists
            SPList destList = destWeb.Lists["Staff Satisfaction"];

            SPListItemCollection listItems = destList.Items;

            SPListItem item = listItems.Add();

            item["Staff Member"] = name;
            item["Team"] = team;

            item.Update();

            destWeb.Dispose();
            destSite.Dispose();
        }

    }
}
 
Back
Top Bottom