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
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/
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
you could perhaps use a temp file name (8 chars) and truncate that?
echo .|dir %temp%
@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 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
> 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
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."
>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)
>@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"
>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%
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.
>> 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.
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
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
>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
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.
They must certainly have a high project success rate with a group name like
that!
/Al
> 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