Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

March 30, 2010

Generating Random Letters

Filed under: C#,Lambda Expressions,VB.NET @ 9:46 pm

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!

7 Comments

  1.   Craig Ison — April 1, 2010 @ 10:12 pm    Reply

    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.

  2.   DeborahK — April 2, 2010 @ 12:42 am    Reply

    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!

  3.   jalii — April 5, 2010 @ 12:35 am    Reply

    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..

  4.   DeborahK — April 5, 2010 @ 9:22 am    Reply

    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.

  5.   heloo — April 6, 2010 @ 10:21 pm    Reply

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

  6.   DeborahK — April 7, 2010 @ 12:05 am    Reply

    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.

  7.   Kevin S Gallagher — January 2, 2011 @ 6:39 pm    Reply

    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!

    _
    Public Function GenerateRandomTextFile( _
    ByVal sender As String, _
    ByVal Length As Integer) As String
    Return sender & GenerateRandomBaseName(Length) & “.TXT”
    End Function
    _
    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

RSS feed for comments on this post. TrackBack URI

Leave a comment

*

© 2014 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs