Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

Random numbers and letters?


(!)

MikeSwim07's Avatar
Computer Specs
Senior Member with 4,629 posts.
THREAD STARTER
 
Join Date: Apr 2007
Location: Cleveland, Ohio
22-Feb-2009, 01:03 PM #1
Random numbers and letters?
Hello,

How can I make a batch file that can create a random string of numbers and letters?

Thanks
-Fabez-'s Avatar
Senior Member with 1,899 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: General
22-Feb-2009, 02:57 PM #2
You can use the random function to generate random numbers.

Code:
%random%
Generating the random letters will be hard in batch, have you considered a different programming language ?
MikeSwim07's Avatar
Computer Specs
Senior Member with 4,629 posts.
THREAD STARTER
 
Join Date: Apr 2007
Location: Cleveland, Ohio
22-Feb-2009, 04:29 PM #3
yeah I have used %random% before. Can you try it with batch to get random letters?
TheOutcaste's Avatar
Computer Specs
Member with 9,028 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
23-Feb-2009, 02:57 AM #4
Not that hard:
Code:
@Echo Off
:: Generates semi-random alphanumeric character, upper/lower case A-z and 0-9
Setlocal EnableDelayedExpansion
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _count=0
:_loop
Set /a _count+=1
SET _RND=%Random%
:: Use (Number of characters-1) as the mod operand. So for just 26 letters use 25 instead of 61
Set /A _RND=_RND%%61
SET _rndalphanum=!_Alphanumeric:~%_RND%,1!
Echo Random character is !_rndalphanum!
If !_count! leq 21 goto _loop
The red lines are just to make it loop and output 20 characters at a time so you can see what it will generate.

HTH

Jerry
MikeSwim07's Avatar
Computer Specs
Senior Member with 4,629 posts.
THREAD STARTER
 
Join Date: Apr 2007
Location: Cleveland, Ohio
23-Feb-2009, 03:48 PM #5
Oh I was wondering like a random string of characters like 8 characters long.
TheOutcaste's Avatar
Computer Specs
Member with 9,028 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
23-Feb-2009, 07:08 PM #6
Here ya go:
Code:
@Echo Off
Setlocal EnableDelayedExpansion
Set _RNDLength=8
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
Echo Random string is !_RndAlphaNum!
I've modified it so you can easily specify the length, and add or remove characters without having to change any other part of the code.
For example, you might not want to use both 0 and O (zero and Uppercase O), or 1 and l (one and lowercase L)
You can use punctuation except for these characters:
! % ^ & < >
You can use ^ and %, but must enter them in the _Alphanumeric variable twice as ^^ or %%. However, if you want to use the result (_RndAlphaNum) later in the batch file (other than Echoing to the screen), they might require special handling.

You can even use a space, as long as it's not the last character in the string. If it ends up as the last character in the generated string though, it will not be used, so you would only have 7 characters.

HTH

Jerry

Last edited by TheOutcaste; 23-Feb-2009 at 07:13 PM..
MikeSwim07's Avatar
Computer Specs
Senior Member with 4,629 posts.
THREAD STARTER
 
Join Date: Apr 2007
Location: Cleveland, Ohio
23-Feb-2009, 08:22 PM #7
Thanks!

Is it possible so that the string created is always 8 characters?
TheOutcaste's Avatar
Computer Specs
Member with 9,028 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
23-Feb-2009, 08:46 PM #8
The length of the resulting string is set by the Set _RNDLength=8 line. As long as you don't change that, or add a space or the 6 punctuation characters (! % ^ & < >) indicated to the Set _AlphaNumeric= line, it will always be 8.

Jerry
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑

Content Relevant URLs by vBSEO 3.3.2