title: Generating a random number to get a random list item
body: <p>I am trying to get a random list item from a "Quotes" list. I currently have a webpart that has a custom property that is used to select the type of quote to be displayed. The "Quotes" list has quotes for the following categories "corporate, Technology and Finance). Currently I am using a foreach loop that displays all quotes for a specific category. I have CAML query to filter the type of quotes that are to be displayed. The value entered in the custom property of the webpart is used in the CAML query to display the quotes.</p>
  
  <p>The next step is to just display a random quote from a specific category, but I am not too sure on how to achieve this. Below is the code I currenlty have the random bit is not yet completed as not to  sure on how to do it.</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
  
  if (this.WebPart != null &amp;&amp; this.WebPart.PracticeArea != null)
  {
  string PracticeArea = this.WebPart.PracticeArea; //get the value of the property
  
  
  
  //getting a reference to the site location
  string webUrl = SPContext.Current.Site.AllWebs["practices"].Url;
  
  //Getting the Quotes list
  using (SPSite site = new SPSite(webUrl))
  {
  using (SPWeb web = site.OpenWeb())
  {
  try
  {
  //getting the Quotes list
  SPList quotesList = web.Lists["Quotes"];
  
  //SPListItemCollection collLisItems = quotesList.Items; not needed
  
  //CAML query to filter or obtain the correct quote based on Area. Value for Area is 
  //passed from the custom property and used in the caml query
  SPQuery quotesbySector = new SPQuery();
  
  //creating an object to handle our random list item selection
  //not too sure whether this is correct
  Random rndQuote = new Random();
  
  int num = rndQuote.Next();
  
  
  
  //string camlquery1 = "&lt;Where&gt;&lt;Eq&gt;" + "&lt;FieldRef Name='Area'/&gt;" + "&lt;/Eq&gt;&lt;/Where&gt;";
  string camlquery1 = @"
  &lt;Where&gt;
  &lt;Eq&gt;
  &lt;FieldRef Name='Area'/&gt;
  &lt;Value Type='Text'&gt;" + PracticeArea + @" &lt;/Value&gt;
  &lt;/Eq&gt;
  &lt;/Where&gt;";
  
  quotesbySector.Query = camlquery1;
  
  SPListItemCollection collListItems = quotesList.GetItems(quotesbySector);
  
  //SPListItem firstQuote = collListItems[0];
  
  //for each loop might need to be removed, as we are only interested in getting a 
  //random quote and not all quotes
  foreach (SPListItem item in collListItems)
  {
  
  string quotes = (string)item["Quote"];
  string quotesSource = (string)item["Source"];
  string quotesYear = (string)item["Year"];
  //string quotesArea = (string)item["Area"]; //not needed used for test purposes
  
  plhQuotes.Controls.Add(new LiteralControl(quotes + "&lt;br/&gt;" + "&lt;br/&gt;" + quotesSource + 
  "&lt;br/&gt;" + "&lt;br/&gt;" + quotesYear + "&lt;br/&gt;" + "&lt;br/&gt;"));
  
  }
  
  
  }
  
  catch (Exception err)
  {
  plhErrors.Controls.Add(new LiteralControl(err.ToString()));
  }
  }
  }
  
  
  
  
  
  
  }
  
  }
  </code></pre>
  
  <p>I am sure there is a simple way to achieve this. Any suggestion would be greatly appreciated it.</p>
  
  <p>Thanks in advance.</p>