Generating Random Letters

Posted Tue, Mar 30 2010 19:46 by Deborah Kurata

Someone recently posted in the forums the need to build a "Word Search" puzzle and wanted to generate a list of random letters.

.NET has all of the features you need to do this in just two lines of code.

In C#:

Random rand  = new Random();
var letter = Enumerable.Range(0, 100).Select(
        i => (char)((int)'A' + rand.Next(0, 26))).ToList();

In VB:

Dim rand As Random = New Random()
Dim letter = Enumerable.Range(0, 100).Select( _
            Function(i) (Chr(Asc("A") + rand.Next(0, 26)))).ToList()

This code first created an instance of the Random class. It then builds the list of random letters.

The Range method on the Enumerable class defines the range of values, in this case the code generates 100 values, starting at 0. So if you need a different number of random values, this is the number you would change.

The Lambda expression uses the ASCII code for letters, adding a random value to "A" to get "A" through "Z". Finally, the code converts the set of letters to a list.

Use this technique whenever you need to generate a set of random letters.

Enjoy!

EDITED 4/1/10: Craig found a bug in the code above. rand.Next(0,25) picks a random number greater than or equal to 0 but LESS THAN 25. So to ensure that "Z" is also used, this needs to be changed to rand.Next(0,26). I corrected the code in both examples above. Thanks Craig!

Filed under: , , , ,

Comments

# re: Generating Random Letters

Thursday, April 01, 2010 10:12 PM by Craig Ison

I found a bug in this code or maybe in the rand.next(min,max) function. The max is rever rever reached. In the code 'Z' will never be returned. The fix is to Change the rand.next(0,25) to rand.next(0,26). Seems wrong but i tested it.

# re: Generating Random Letters

Friday, April 02, 2010 12:42 AM by Deborah Kurata

Hi Craig -

Thanks for the correction. I re-read the documentation and you are correct. max really defines max-1. I corrected the code.

Thanks again!

# re: Generating Random Letters

Monday, April 05, 2010 12:35 AM by jalii

sorry but for the code above you have given me, for the fuction the expression is underline. it wrotes that the expression expected. and for the .tolist() it writes end of statement expected. thanks..

# re: Generating Random Letters

Monday, April 05, 2010 9:22 AM by Deborah Kurata

Hi Jalii -

Check your parenthesis and ensure you have the correct number of them. And ensure you are targeting the .NET 3.5 framework.

Hope this helps.

# re: Generating Random Letters

Tuesday, April 06, 2010 10:21 PM by heloo

i dont know what is parenthesis and correct number. im using vb 2005. so i dont know what is a .net3.5

# re: Generating Random Letters

Wednesday, April 07, 2010 12:05 AM by Deborah Kurata

Hi heloo -

The code posted here only works with .NET 3.5, which is the version of .NET targetted by Visual Studio 2008. So it won't work in VB 2005.

# re: Generating Random Letters

Sunday, January 02, 2011 6:39 PM by Kevin S Gallagher

I implemented a random file name with your code as shown below using language extensions which might seem odd at first since to call the extension one might do

"".GenerateRandomFile(8)

But if you want to prefix it we can

"xzy".GenerateRandomFile(8)

Any ways thanks!

   <System.Runtime.CompilerServices.Extension()> _

   Public Function GenerateRandomTextFile( _

       ByVal sender As String, _

       ByVal Length As Integer) As String

       Return sender & GenerateRandomBaseName(Length) & ".TXT"

   End Function

   <System.Runtime.CompilerServices.Extension()> _

   Public Function GenerateRandomXmlFile( _

       ByVal sender As String, _

       ByVal Length As Integer) As String

       Return sender & GenerateRandomBaseName(Length) & ".XML"

   End Function

   Private Function GenerateRandomBaseName( _

       ByVal Length As Integer) As String

       Dim rand As Random = New Random()

       Return CStr(Enumerable.Range(0, Length) _

               .Select(Function(i) (Chr(Asc("A") + _

                       rand.Next(0, 26)))).ToArray)

   End Function

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: