locked
Generate Random Number for SharePoint list Item RRS feed

  • Question

  • Hi All,

           I have list with Item "Random Number" and   I need to generate the random number and store in SharePoint List Item "Random Number" .Can any one tell me how can I do it.


    Thanks,

    Quality Communication Provides Quality Work.

    Mohammad Siddiqali.

    • Edited by SP Assest Monday, January 30, 2012 6:13 AM
    Monday, January 30, 2012 5:29 AM

Answers

  • You can use the following code if you want a random number as soon as someone adds an item.

    public override void ItemAdding(SPItemEventProperties properties)
    {


    Random random = new Random();

    int rand = random.Next(min, max);//Where min and max are values you need a random number in between
    using (SPWeb web = properties.OpenWeb())
    {
    Name = web.Lists[properties.ListId].Fields["<Name of the column>"].InternalName;

    }
    properties.AfterProperties[Name] = rand.ToString();
    }


    Varun Malhotra
    =================
    Please Mark As Answer if my post solves your problem or Vote As Helpful if a post has been helpful for you.
    • Proposed as answer by servy42 Monday, January 30, 2012 6:14 PM
    • Marked as answer by Shimin Huang Monday, February 6, 2012 7:53 AM
    Monday, January 30, 2012 6:55 AM

All replies

  • I don't believe that there is a way to do this within the SharePoint UI.

    I would create a SharePoint event for the field that would programatically assign the random number whenever the event is fired (i.e. list item updated, list item created, etc...)

    You could even use the .Net function - Rand() for this.


    Varun Malhotra
    =================
    Please Mark As Answer if my post solves your problem or Vote As Helpful if a post has been helpful for you.
    Monday, January 30, 2012 5:32 AM
  • Hi Varun,

               Thanks  for your blaze response.Can you provide me event information to generate the random number for list item.


    Thanks, Quality Communication Provides Quality Work. Mohammad Siddiqali.
    Monday, January 30, 2012 5:37 AM
  • When exactly do want to assign the numbers? .... when the item is added or just loop the whole list ... i assume you want to assign the random numbers in a particular field
    "T" | My blog updates | My Twitter | Our Products | Mail Me | LinkedIn | My Virtual Business Card

    A THOUGHT ABOUT MY WORKPLACE :)
    Monday, January 30, 2012 5:53 AM
  • You can use the following code if you want a random number as soon as someone adds an item.

    public override void ItemAdding(SPItemEventProperties properties)
    {


    Random random = new Random();

    int rand = random.Next(min, max);//Where min and max are values you need a random number in between
    using (SPWeb web = properties.OpenWeb())
    {
    Name = web.Lists[properties.ListId].Fields["<Name of the column>"].InternalName;

    }
    properties.AfterProperties[Name] = rand.ToString();
    }


    Varun Malhotra
    =================
    Please Mark As Answer if my post solves your problem or Vote As Helpful if a post has been helpful for you.
    • Proposed as answer by servy42 Monday, January 30, 2012 6:14 PM
    • Marked as answer by Shimin Huang Monday, February 6, 2012 7:53 AM
    Monday, January 30, 2012 6:55 AM
  • You can generate a random number for particular column by using Event Receiver as  Varun Malhotra said ,so the following code will show how generate random number  at the adding new item to the list

    namespace EventReceiverDesarrolloLP.EventReceiver1
    {
        public class EventReceiver1 : SPItemEventReceiver
        {
           public override void ItemAdding(SPItemEventProperties properties)
           {
               Random random = new Random();
               Min is 0 and Max is 100
              int randomNumber = random.Next(0, 100);
               //Your column name
               properties.AfterProperties["RandomNumber"]= randomNumber;
    
               //No need to call Update() method
              base.ItemAdding(properties);
           }
        }
    }
    
    
    


    Hope That help.

    Regards.


    Ahmed Naji SharePoint Geek
    MCP|MCTS
    My English SharePoint Blog | DotnetFinder My Arabic SharePoint Blog| CodeReloaded
    My Gallery Contributions
    SharePoint 2010 Twitter Web Part
    SharePoint 2010 Custom Timer Job
    Monday, January 30, 2012 10:26 AM
  • Being a novice on Sharepoint, where does the code go in order to work.

    i.e. is it in a Content web part, or is it necessary to build a DVWP with SP Designer and add the code there?

    All of these kinds of answers assumes the reader who finds this information via web searches knows what to do with the code.  I am particularily interested in creating a random number when a new item is added to a list.

    hnp@gammai.com


    HParsonage

    Saturday, October 13, 2012 10:04 PM
  • Hi,

    I have a codeless solution that may help in some cases. If you have another field in your list that contains a number (or even date), you can add a calculated column and use the "MOD" function with that number (or date) filed as the input.

    for example if you need a random number between 1 & 10, you can use this formula (assuming that your list items creation date/time is random):

    =MOD([Created], 10)

    Wednesday, October 17, 2012 8:10 AM
  • Hi,

    The thread is very old, but if someone is still interested...

    As it was described here - you can use the part of the date value to generate random value

    This method did not work for me. Each time I used RIGHT or LEFT functions - error appeared

    The way I found is: 

    =INT(((VALUE(Created)-INT(VALUE(Created)))*10000-INT((VALUE(Created)-INT(VALUE(Created)))*10000))*1000000000)

    this: VALUE(Created)-INT(VALUE(Created)) - removes days

    this: ((VALUE(Created)-INT(VALUE(Created)))*10000-INT((VALUE(Created)-INT(VALUE(Created)))*10000)) - removes up to seconds

    Last big multiplier (1000000000) is a range of random numbers you need to use

    Finally you get a pseudo random value, which is  based on milli-, micro-, nano-, piko- and etc. seconds


    Paul


    • Edited by Neobuh Friday, July 6, 2018 6:59 AM
    Friday, July 6, 2018 6:57 AM