﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming By A Tool &#187; Random</title>
	<atom:link href="http://byatool.com/tag/random/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>This is my recorded stumbling through ASP.Net, Framework 3.5, C# 3.0, Ajax, Javascript, and Love.  Stay, learn, destroy.  It's your life.</description>
	<lastBuildDate>Thu, 03 May 2012 17:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Random String of Specified Length with Enumerable</title>
		<link>http://byatool.com/utilities/random-string-of-specified-length-with-enumerable/</link>
		<comments>http://byatool.com/utilities/random-string-of-specified-length-with-enumerable/#comments</comments>
		<pubDate>Thu, 26 Jun 2008 16:30:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Random]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/06/26/random-string-of-specified-length-with-enumerable/</guid>
		<description><![CDATA[using System; using System.Collections.Generic; using System.Linq; using System.Text; public static String RandomString(Int32 length) { Random randomGenerator; String returnValue; randomGenerator = new Random(); returnValue = new string(Enumerable.Range(0, length).Select(i =&#62; (char)('A' + randomGenerator.Next(0, 25))).ToArray()); return returnValue; } What this does: Enumerable.Range basically says, &#8220;Give me a collection of numbers from the first parameter to the second&#8221;, or [...]]]></description>
			<content:encoded><![CDATA[<pre><strong>
<span style="color: #33cccc;">using</span> System;
<span style="color: #33cccc;">using</span> System.Collections.Generic;
<span style="color: #33cccc;">using</span> System.Linq;
<span style="color: #33cccc;">using</span> System.Text;

<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #33cccc;">String</span> RandomString(<span style="color: #33cccc;">Int32</span> length)
{
 <span style="color: #33cccc;">Random</span> randomGenerator;
 <span style="color: #33cccc;">String</span> returnValue;

  randomGenerator = <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">Random</span>();

  returnValue = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">string</span>(<span style="color: #33cccc;">Enumerable</span>.Range(0, length).Select(i =&gt; (<span style="color: #0000ff;">char</span>)(<span style="color: #ff0000;">'A'</span> + randomGenerator.Next(0, 25))).ToArray());

 <span style="color: #0000ff;">return</span> returnValue;
}
</strong></pre>
<p>What this does:</p>
<p>Enumerable.Range basically says, &#8220;Give me a collection of numbers from the first parameter to the second&#8221;, or in this case from 0 to length.</p>
<p>Select basically is a method that takes in an anonymous method (Lamdba expression in this case), goes through each of the items in the collection, and runs the anonymous method for every item in the collection. Not exactly sure what Select does specifically, but it is most likely something like this: (roughish psuedo code)</p>
<pre><strong>
<span style="color: #33cccc;">public</span> <span style="color: #0000ff;">static</span> Array Select(<span style="color: #33cccc;">Func</span> func))
{

 <span style="color: #33cccc;">Array</span> returnValue;

 <span style="color: #0000ff;">foreach</span>(<span style="color: #33cccc;">Int32</span> currentItem <span style="color: #0000ff;">in</span> Items)
 {
    returnValue.Add(func());
 }

 return returnValue;

}
</strong></pre>
<p>Where Func is:</p>
<pre><strong>
<span style="color: #0000ff;">public</span> <span style="color: #33cccc;">Char</span> Func()
{
 return  (<span style="color: #0000ff;">char</span>)(<span style="color: #ff0000;">'A'</span> + randomGenerator.Next(0, 25);
}
</strong></pre>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/utilities/random-string-of-specified-length-with-enumerable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random Enumeration Generator with Generics</title>
		<link>http://byatool.com/general-coding/random-enumeration-generator-with-generics/</link>
		<comments>http://byatool.com/general-coding/random-enumeration-generator-with-generics/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 21:04:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Enumeration]]></category>
		<category><![CDATA[Random]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/06/25/random-enumeration-generator-with-generics/</guid>
		<description><![CDATA[public static I RandomEnumeration&#60;I&#62;() { I enumerationToCheck; Int32 indexToUse; String[] names; //Use activator to create an instance of the type I enumerationToCheck = System.Activator.CreateInstance&#60;I&#62;(); //Make sure the instance is an Enumeration //Unfortunately you can't check that in the method //delcaring using "which". if (enumerationToCheck as Enum == null) { throw new InvalidOperationException(); } //Get the [...]]]></description>
			<content:encoded><![CDATA[<pre><strong>
public static I RandomEnumeration&lt;I&gt;()
{
  I enumerationToCheck;
  Int32 indexToUse;
  String[] names;

  <span style="color: #009900;">//Use activator to create an instance of the type I</span>
  enumerationToCheck = System.Activator.CreateInstance&lt;I&gt;();

  <span style="color: #009900;">//Make sure the instance is an Enumeration</span>
<span style="color: #009900;">   //Unfortunately you can't check that in the method </span>
<span style="color: #009900;">   //delcaring using "which".</span>
  if (enumerationToCheck as Enum == null)
  {
    throw new InvalidOperationException();
  }

  <span style="color: #009900;">//Get the list of the enumeration item names</span>
  names = Enum.GetNames(typeof(I));

  if (names.Length &gt; 0)
  {
    <span style="color: #009900;">//Grab a random name within the boundaries of the</span>
<span style="color: #009900;">     //names collection.</span>
    indexToUse = RandomInt32(0, names.Length);
    <span style="color: #009900;">//parse the name to create the random enum</span>
    enumerationToCheck = (I)Enum.Parse(typeof(I), names[indexToUse]);
  }

  return enumerationToCheck;

}
</strong></pre>
<p>Usage:</p>
<pre><strong>
  SomeEnum test = RandomEnumeration();
</strong></pre>
<p>Why bother? For unit testing and creating test classes. Possibly<br />
for defaults on an enumeration, but not really needed since<br />
they are value types. Oh yeah AND BECAUSE I FELT LIKE IT. I don&#8217;t<br />
have to explain myself to you.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/random-enumeration-generator-with-generics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

