Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Generate a random alphanumeric string

3,139 views
Skip to first unread message

Jonny

unread,
Apr 23, 2005, 8:58:24 AM4/23/05
to
Hi,

I would like to be able to generate random passwords for new users.

Please could you tell me if it is possible to generate a random
six-character alphanumeric string.

Thanks for your help.

Regards,
Jonny

William Allen

unread,
Apr 23, 2005, 9:32:24 AM4/23/05
to
"Jonny" wrote in message

You could use the WSH random function to lookup an A-Z0-9 list,
or for a third-party solution try WAPG here:
http://www.adel.nursat.kz/apg/download.shtml

WAPG has the useful option of generating "pronounceable"
passwords, though exactly how pronounceable is perhaps
a matter of opinion and your own lingual dexterity.

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/


Timo Salmi

unread,
Apr 23, 2005, 10:08:08 AM4/23/05
to
Jonny wrote:
> I would like to be able to generate random passwords for new
> users.

This is not a complete answer, but you might find of interest

87} How can I create a four-digit random string?

116469 Apr 19 22:46 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

Mike Jones

unread,
Apr 23, 2005, 10:35:47 AM4/23/05
to

you could perhaps use a temp file name (8 chars) and truncate that?
echo .|dir %temp%


Herbert Kleebauer

unread,
Apr 23, 2005, 11:10:48 AM4/23/05
to
Jonny wrote:
>
> I would like to be able to generate random passwords for new users.
>
> Please could you tell me if it is possible to generate a random
> six-character alphanumeric string.

@echo off
set pas=
set s=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
set m=0
:loop
set /a n=%random% %% 62
call set pas=%pas%%%s:~%n%,1%%
set /a m=m+1
if not %m%==6 goto loop:
echo %pas%

Jonny

unread,
Apr 23, 2005, 12:43:53 PM4/23/05
to
I wrote:

> I would like to be able to generate random passwords for new users.
>
> Please could you tell me if it is possible to generate a random
> six-character alphanumeric string.


I went with Herbert's solution:

@echo off
set pas=
set s=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
set m=0
:loop
set /a n=%random% %% 62
call set pas=%pas%%%s:~%n%,1%%
set /a m=m+1
if not %m%==6 goto loop:
echo %pas%

I didn't know about %random%. Nice one Herbert.

William suggested WAPG at:

http://www.adel.nursat.kz/apg/download.shtml

which I tried. I see what you mean William about the pronounceable
option. Some of the passwords it gave me were:

Kiechcead0 (Kiech-cead-ZERO)
Moyshnic+ (Moy-shnic-PLUS_SIGN)
wurherlyet (wur-herl-yet)
meardufJu (meard-uf-Ju)
yunafIad (yun-af-Iad)
Jadsigbeb (Jads-ig-beb)

Thanks for all your replies.

Regards,
Jonny


Jonny

unread,
Apr 23, 2005, 12:46:04 PM4/23/05
to
I wrote:

> I would like to be able to generate random passwords for new users.
>
> Please could you tell me if it is possible to generate a random
> six-character alphanumeric string.

Timo Salmi

unread,
Apr 23, 2005, 1:52:51 PM4/23/05
to
Herbert Kleebauer wrote:
>> I would like to be able to generate random passwords for new
>> users.

> set /a n=%random% %% 62

Well done. This is a prime example why it pays to follow the
solutions from the regulars. There are so many tricks and details
that one just can't know them all, even if they are well documented
once one knows to search for them.

"%RANDOM% System Returns a random decimal number between 0 and
32767. Generated by Cmd.exe."

Dr John Stockton

unread,
Apr 23, 2005, 3:28:28 PM4/23/05
to
JRS: In article <426A6578...@unibwm.de>, dated Sat, 23 Apr 2005
17:10:48, seen in news:alt.msdos.batch.nt, Herbert Kleebauer
<kl...@unibwm.de> posted :

>Jonny wrote:
>>
>> I would like to be able to generate random passwords for new users.
>>
>> Please could you tell me if it is possible to generate a random
>> six-character alphanumeric string.

>set /a n=%random% %% 62

Unless qualified, "Random" means "equally distributed". Since the
number of possible values of %random% is not a multiple of 62, your
method cannot give a truly equal distribution, presuming %random% itself
to be good.

Since the difference in this case will be of the order of 0.2% it
probably does not matter. Nevertheless, the algorithm of that line is
flawed, and it should not be employed without due care for other
purposes.

If R = %random% sets R to an integer in 0..(Q-1) inclusive, I believe
that the correct method may be to use Floor(R*62/Q) but, though I've
lightly tested that, I've not verified it in a trustworthy manner. I'd
hope to find it in Knuth, if I owned a copy.

--
Š John Stockton, Surrey, UK. *@merlyn.demon.co.uk / ??.Stoc...@physics.org Š
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)

Clay Calvert

unread,
Apr 23, 2005, 7:38:24 PM4/23/05
to
On Sat, 23 Apr 2005 17:10:48 +0200, Herbert Kleebauer <kl...@unibwm.de>
wrote:

>@echo off
>set pas=
>set s=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
>set m=0
>:loop
>set /a n=%random% %% 62
>call set pas=%pas%%%s:~%n%,1%%
>set /a m=m+1
>if not %m%==6 goto loop:
>echo %pas%

Very clever. I was going to post a script that creates a file for
Excel to create random passwords until I saw yours.

Pardon me for setting it up to take advantage of delayed variable
expansion.

@echo off
setlocal enabledelayedexpansion
set pas=
set s=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
for /l %%a in (1,1,6) do (
set /a n=!random! %% 62
call set pas=!pas!%%s:~!n!,1%%)
echo %pas%

Though not requested by the OP, adding a # and $ to the end of S would
make the string an even power of 2, and the 'randomness' would be more
accurate as John pointed out.

Thanks

Clay Calvert
CCal...@Wanguru.com
Replace "W" with "L"

Clay Calvert

unread,
Apr 23, 2005, 10:06:42 PM4/23/05
to
On Sat, 23 Apr 2005 19:38:24 -0400, Clay Calvert
<ccal...@Wanguru.com> wrote:

>On Sat, 23 Apr 2005 17:10:48 +0200, Herbert Kleebauer <kl...@unibwm.de>
>wrote:

If it is necessary to verify that the password has at least one item
from upper, lower and number(character) then this could be used.

@echo off
setlocal enabledelayedexpansion
set U=ABCDEFGHIJKLMNOPQRSTUVWXYZ
set L=abcdefghijklmnopqrstuvwxyz
set N=1234567890#$
set S=%U%%L%%N%
:loop
set PW=


for /l %%a in (1,1,6) do (

set /a x=!random! %% 64
call set PW=!PW!%%s:~!x!,1%%)

for %%a in (%U% %L% %N%) do (
echo %PW% |findstr [%%a] >nul
if errorlevel 1 goto:loop)
echo %PW%

William Allen

unread,
Apr 24, 2005, 12:45:30 AM4/24/05
to
"Dr John Stockton" wrote in message
...snip

> If R = %random% sets R to an integer in 0..(Q-1) inclusive, I believe
> that the correct method may be to use Floor(R*62/Q) but, though I've
> lightly tested that, I've not verified it in a trustworthy manner. I'd
> hope to find it in Knuth, if I owned a copy.

By "Knuth", I assume you mean the _Second_ Volume of the
Art of Computer Programming: Seminumerical Algorithms,
and in particular, Chapter 3 - Random Numbers.

In my edition: ISBN 0-201-03822-6 Hardback (1961,1981),
Knuth in "3.2.1 Linear Congruential Method" under
"3.2.1.1 Choice of modulus" is silent on the point. Knuth
mainly concerns himself in this and related sections with
recursive generation of the form X.n+1 = (aX.n + c) mod m
and how - mostly not - to improve it.

This is not quite what is happening with the 62 modulus here. If you
clarify the point you're uncertain about, I'll look it up for you, time permitting.

Timo Salmi

unread,
Apr 24, 2005, 12:11:33 PM4/24/05
to
Dr John Stockton wrote:
> JRS: In article <426A6578...@unibwm.de>, dated Sat, 23 Apr
> 2005 Herbert Kleebauer <kl...@unibwm.de> posted :

>> Jonny wrote:
>>> I would like to be able to generate random passwords for new
>>> users.

>> set /a n=%random% %% 62

> Since the difference in this case will be of the order of 0.2% it


> probably does not matter. Nevertheless, the algorithm of that
> line is flawed, and it should not be employed without due care
> for other purposes.

The random number orthodoxy is important in large scale simulations
such as Monte Carlo simulations for economic systems. In fact it is
a very hot issue in discussing the scientific validity of simulated
results. In fact so much so that huge random number sets are sold
even commercially.

Now, the purpose stated clearly is not for such a case. The
deviation from true randomness does not then matter. The point made
by Clay Calvert, however, is important. That is that special
characters should be included in the base set where the passwords
are drawn from. In fact many password changing programs will reject
passwords without that feature. The inclusion of the special
characters into the inventive solutions presented is not quite
trivial though for plain scripting. This is because many if not most
of the special characters are "poison characters" for CMD.EXE.

Timo Salmi

unread,
Apr 25, 2005, 3:00:14 AM4/25/05
to
By a coincidence there was and is a totally unrelated discussion with
some of the same features going on in a general Finnish programming
newsgroup about generating random letters. That discussion is about
c++. Especially if one programs under different platforms, the
various solutions can be of generic interest. Occasionally the
solutions could even crosspollinate. As an afterthought I'll paste
below one of the brief solutions for comparison. In a task like this
the central ideas, in this case the $RANDOM % $len and picking from
the string, are the base for the realization of the particular code.
The code snippet below is due to Jari Vuoksenranta.

str="abcdefghijklm"
len=`expr length $str + 1`
for i in `seq 100`; do
echo -n `expr substr $str \( $RANDOM % $len \) 1`;
done

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland

Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

William Allen

unread,
Apr 25, 2005, 11:03:01 AM4/25/05
to
"Timo Salmi" wrote in message
> Dr John Stockton wrote:
> > Herbert Kleebauer posted :

> >> Jonny wrote:
> >>> I would like to be able to generate random passwords for new
> >>> users.
>
> >> set /a n=%random% %% 62
>
> > Since the difference in this case will be of the order of 0.2% it
> > probably does not matter. Nevertheless, the algorithm of that
> > line is flawed, and it should not be employed without due care
> > for other purposes.
>
> The random number orthodoxy is important in large scale simulations
> such as Monte Carlo simulations for economic systems. In fact it is
> a very hot issue in discussing the scientific validity of simulated
> results. In fact so much so that huge random number sets are sold
> even commercially.
>
> Now, the purpose stated clearly is not for such a case. The
> deviation from true randomness does not then matter.
...snip (reference to Clay Calvert's extension of the problem/solution)

It seems to me that, in Herbert Kleebauer's solution for the problem
exactly as posed, his algorithm:


set /a n=%random% %% 62

can be replaced by:
set /a n=(%random%*65535)/34635248

This reduces the Bias to around .00009% without the need
for floating point arithmetic functions (not available in SET /a).
At this level it's unlikely to matter for most purposes.

Note:
Bias calculation for algorithm above:
Bias = 62 - (32767*65535)/34635248 = 0.000000895
or around 0.00009%

(a) Reason for 65535 factor: it's the largest permitted
without integer overflow in SET /a for max(%random%).
Max(%random%)=32767
(b) Reason for 34635248 factor: it's the smallest integer that
then produces an integer division result not exceeding 61.

The meaning of "Bias" in the above: the deficiency in the
proportion of occurrences of the _final_ character in the string
used in the SET S= statement in Herbert Kleebauer's solution
compared with the proportions of any other character. So in
the case posted, "0" would be expected to appear around
.00009% fewer times in the long run than any other character.
This may be better than the "randomness" of %RANDOM% itself.

Note also that in some older version of CMD.EXE (such as was
used to produce WIN95CMD.EXE), Herbert Kleebauer's line
set /a m=m+1
may fail in a batch file (though not in immediate mode). This despite
it's being syntactically correct and in accord with SET /? instructions.
The fix is:
set /a m=%m%+1

Dr John Stockton

unread,
Apr 25, 2005, 2:36:46 PM4/25/05
to
JRS: In article <426c9580$1...@news.dnainternet.net>, dated Mon, 25 Apr
2005 10:00:14, seen in news:alt.msdos.batch.nt, Timo Salmi <t...@uwasa.fi>
posted :

>str="abcdefghijklm"
>len=`expr length $str + 1`
>for i in `seq 100`; do
> echo -n `expr substr $str \( $RANDOM % $len \) 1`;
>done

Well, it's easier to read than Finnish (for most of us here). But, with
variable $len (13?) presumably not being a factor of the number of
possible values of entity $RANDOM (if that's integer), then the
values of $RANDOM mod $len (if that's what it means) cannot be
evenly distributed, although it may be good enough for many purposes.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Delphi 3 Turnpike 4 ©
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htm> clpdmFAQ;
<URL:http://www.borland.com/newsgroups/guide.html> news:borland.* Guidelines

Dr John Stockton

unread,
Apr 24, 2005, 3:57:54 PM4/24/05
to
JRS: In article <v8ml611d7hjkdj59r...@4ax.com>, dated
Sat, 23 Apr 2005 19:38:24, seen in news:alt.msdos.batch.nt, Clay Calvert
<ccal...@Wanguru.com> posted :

>
>Though not requested by the OP, adding a # and $ to the end of S would
>make the string an even power of 2, and the 'randomness' would be more
>accurate as John pointed out.

Over here, the even powers of 2 are 1, 4, 16, 64, ..., and we would put
"... the length of the string an integer power of 2, ...".

Provided that the correct interpretation of what Timo quoted is not what
the words actually mean in English, yes. The integers between 0 and
32767 are 1..32766, but the intent is probably 0..32767.

But that is adjusting the "need" to suit the inferior method, which is
not always possible; it is better to have a more generally satisfactory
method that can handle all output ranges correctly.

The expression that I posted yesterday as possibly correct has not
passed further testing; and has done so in a manner which indicates a
proof that no such expression can be truly satisfactory in the general
case.

Given a function Random(Q) returning a random integer in the range
0..(Q-1), to get a random integer in the range 0..(P-1) [for which
P<=Q is essential] one should use something like

repeat X := Random(Q) until X < n*P ; X := X mod P ;

where n is any positive integer such that n*P <= Q, preferably the
largest such.

Javascript test, Q=18, P=7 :
function Random(x) { return Math.floor(x*Math.random()) }
A = []
K = 21000 ; while (K--) {
do { J = Random(18) } while (J >= 14) ; J %= 7 // generate
if (!A[J]) A[J] = 0 ; A[J]++ } // count
A // show

But I recommend further consideration and testing ...

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Al Dunbar

unread,
Apr 26, 2005, 9:18:02 PM4/26/05
to

"Timo Salmi" <t...@uwasa.fi> wrote in message
news:426c9580$1...@news.dnainternet.net...

> By a coincidence there was and is a totally unrelated discussion with
> some of the same features going on in a general Finnish programming
> newsgroup

They must certainly have a high project success rate with a group name like
that!

/Al

Timo Salmi

unread,
Apr 26, 2005, 11:26:08 PM4/26/05
to
Al Dunbar wrote:

> "Timo Salmi" <t...@uwasa.fi> wrote:
>> some of the same features going on in a general Finnish
>> programming newsgroup

> They must certainly have a high project success rate with a group
> name like that!

In fact they (we) do. Please bear in mind the extent of the potential
reader space. Using Finnish selects the participants perhaps a weeny
bit more than using English. :-)

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland

lyk...@gmail.com

unread,
May 10, 2015, 4:17:04 PM5/10/15
to
Hello ! Nice to meet you ! I have trouble coding in cmd. I don't know you can help me or not. I want to create one batch file that randomly return any words (for example, Head and Foot). When I type that batch file name in cmd three time it displays randomly one of two words. Can you tell me how to get code.

Todd Vargo

unread,
May 11, 2015, 9:32:52 AM5/11/15
to
This reads like - feed me code to create yet another spam generator.


JJ

unread,
May 11, 2015, 11:07:15 AM5/11/15
to
On Sun, 10 May 2015 13:17:03 -0700 (PDT), lyk...@gmail.com wrote:
> Hello ! Nice to meet you ! I have trouble coding in cmd. I don't know you can help me or not. I want to create one batch file that randomly return any words (for example, Head and Foot). When I type that batch file name in cmd three time it displays randomly one of two words. Can you tell me how to get code.

Here ya go.

@ECHO OFF
REM HEAD
%0
REM FOOT

toofly....@gmail.com

unread,
Jan 19, 2018, 9:11:15 PM1/19/18
to
Proper code for this.

@echo off
echo.
echo head.
echo.
echo foot
Pause
Exit




If you want a pause between the two options
@echo off
echo.
echo head.
TIMEOUT 5 /nobreak
echo.
echo foot
TIMEOUT 3 /nobreak
echo press enter to exit
pause>nul
exit
The numeral 5 being 5 seconds <<don't write this in code


0 new messages