locked
How to generate the random names using the vbscript? RRS feed

  • Question

  • Hi ,
           I want to generate the Random names which i want to use in my testing.

    For example.

    Using a name like "madhusudhan" as Random name , then i want to generate random names up to maximum and expot to that names to the excel sheet .



    Regards

    Madhu


    Friday, January 8, 2010 5:55 PM

Answers

  • If I understand, you can use the Rnd function to generate random integers, then use these to select a character from a list of allowed characters and construct a name. A quick example follows:
    Option Explicit
    
    Dim intMax, iLoop, k, intValue, strChar, strName, intNum
    
    ' Specify the alphabet of characters to use.
    Const Chars = "abcdefghijklmnopqrstuvwxyz"
    
    ' Specify length of names.
    intMax = 10
    
    ' Specify number of names to generate.
    intNum = 20
    
    Randomize()
    For iLoop = 1 To intNum
        strName = ""
        For k = 1 To intMax
            ' Retrieve random digit between 0 and 25 (26 possible characters).
            intValue = Fix(26 * Rnd())
            ' Convert to character in allowed alphabet.
            strChar = Mid(Chars, intValue + 1, 1)
            ' Build the name.
            strName = strName & strChar
        Next
    
        Wscript.Echo strName
    Next
    
    It will be easy to run this at a command prompt and redirect the output to a text file that can be imported into a spreadsheet. For example, if the VBScript program is RandomNames.vbs, use the following at a command prompt:

    cscript //nologo RandomNames.vbs > Names.txt

    The file Names.txt will have one name per line. This is much easier than adding all the extra code to populate a spreadsheet, and gives you more flexibility (you can paste the names anywher you like for example).

    Richard Mueller
    MVP ADSI

    • Marked as answer by Madhu1981 Friday, January 8, 2010 7:05 PM
    • Edited by Richard MuellerMVP Friday, September 28, 2012 7:00 PM Updated code
    Friday, January 8, 2010 6:14 PM

All replies

  • If I understand, you can use the Rnd function to generate random integers, then use these to select a character from a list of allowed characters and construct a name. A quick example follows:
    Option Explicit
    
    Dim intMax, iLoop, k, intValue, strChar, strName, intNum
    
    ' Specify the alphabet of characters to use.
    Const Chars = "abcdefghijklmnopqrstuvwxyz"
    
    ' Specify length of names.
    intMax = 10
    
    ' Specify number of names to generate.
    intNum = 20
    
    Randomize()
    For iLoop = 1 To intNum
        strName = ""
        For k = 1 To intMax
            ' Retrieve random digit between 0 and 25 (26 possible characters).
            intValue = Fix(26 * Rnd())
            ' Convert to character in allowed alphabet.
            strChar = Mid(Chars, intValue + 1, 1)
            ' Build the name.
            strName = strName & strChar
        Next
    
        Wscript.Echo strName
    Next
    
    It will be easy to run this at a command prompt and redirect the output to a text file that can be imported into a spreadsheet. For example, if the VBScript program is RandomNames.vbs, use the following at a command prompt:

    cscript //nologo RandomNames.vbs > Names.txt

    The file Names.txt will have one name per line. This is much easier than adding all the extra code to populate a spreadsheet, and gives you more flexibility (you can paste the names anywher you like for example).

    Richard Mueller
    MVP ADSI

    • Marked as answer by Madhu1981 Friday, January 8, 2010 7:05 PM
    • Edited by Richard MuellerMVP Friday, September 28, 2012 7:00 PM Updated code
    Friday, January 8, 2010 6:14 PM
  • Hey Richard its working fine san!!!!

    Thanks alot Richard/

    U can close the thread..i got the expected answers.
    • Marked as answer by Madhu1981 Friday, January 8, 2010 7:05 PM
    • Unmarked as answer by Madhu1981 Friday, January 8, 2010 7:05 PM
    Friday, January 8, 2010 7:05 PM
  • Richard, I just wanted to point out what I think you intended the use of the 'intNum' variable, as the maximum number of strings to generate,

    so touched the code, just to reflect your intention (the changes are hightlighted):

    Dim intMax, iLoop, k, intValue, strChar, strName, intNum

    ' Specify the alphabet of characters to use.
    Const Chars = "abcdefghijklmnopqrstuvwxyz"
    ' Specify length of names.
    intMax = 10
    ' Specify number of names to generate.
    intNum = 20

    Randomize()
    For iLoop= 1 To intNum
        strName = ""
        For k = 1 To intMax
            ' Retrieve random digit between 0 and 25 (26 possible characters).
            intValue = Fix(26 * Rnd())
            ' Convert to character in allowed alphabet.
            strChar = Mid(Chars, intValue + 1, 1)
            ' Build the name.
            strName = strName & strChar
        Next

        Wscript.Echo strName
    Next

    Friday, September 28, 2012 1:53 PM
  • Yes, very good catch. Thanks for the update. I will update the code.


    Richard Mueller - MVP Directory Services


    Friday, September 28, 2012 6:57 PM