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

random ascii string %width% chars wide

41 views
Skip to first unread message

Mike Sanders

unread,
Apr 16, 2017, 5:38:41 PM4/16/17
to
Some brainfood from a project I'm working on, hope its useful:


@echo off & setlocal enabledelayedexpansion

:: var = %RANDOM% * (%max% - %min% + 1) / 32768 + %min%

set /a "width=8"

@cls

:: scope a-z
for /l %%x in (1,1,%width%) do (
set /a "ascii = !random! * (122 - 97 + 1) / 32768 + 97"
cmd /c exit /b !ascii!
set "lower=!lower!!=ExitCodeAscii!"
)

echo random lowercase ascii %width% chars wide: %lower%

:: scope A-Z
for /l %%x in (1,1,%width%) do (
set /a "ascii = !random! * (90 - 65 + 1) / 32768 + 65"
cmd /c exit /b !ascii!
set "upper=!upper!!=ExitCodeAscii!"
)

echo random uppercase ascii %width% chars wide: %upper%

:: scope 0-9
for /l %%x in (1,1,%width%) do (
set /a "num = !random! * (9 - 1) / 32768"
set "digits=!digits!!num!"
)

echo random digits %width% chars wide: %digits%

exit /b


--
later on,
Mike

https://busybox.hypermart.net

Mike Sanders

unread,
Apr 16, 2017, 5:56:53 PM4/16/17
to
Mike Sanders <mi...@porkchop.bsd> wrote:

> set /a "num = !random! * (9 - 1) / 32768"

slight tweak...

set /a "num = !random! * 10 / 32768"

Herbert Kleebauer

unread,
Apr 17, 2017, 3:40:32 AM4/17/17
to
On 16.04.2017 23:35, Mike Sanders wrote:
> Some brainfood from a project I'm working on, hope its useful:

Wouldn't it be simpler to just define the allowed characters in
a string:

@echo off
set width=8

set s=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
set n=62

set ras=
for /l %%i in (1,1,%width%) do call :sub
echo %ras%
goto :eof

:sub
set /a r = %random% * %n% / 32768
call set ras=%ras%%%s:~%r%,1%%

Mike Sanders

unread,
Apr 17, 2017, 10:13:03 AM4/17/17
to
Herbert Kleebauer <kl...@unibwm.de> wrote:

> Wouldn't it be simpler to just define the allowed characters in
> a string:

Yes, its much simpler Herbert. I'm using the construct below,
which is not unlike what you've suggested (I've chosen to keep
most all of it within the loop however & feel the sub routine
is unneeded for my needs):


@echo off & setlocal enabledelayedexpansion

if %1.==. (
:: I need to test not only for the existence of %1,
:: but ensure its numeric only, still working on this...
exit /b 1
)

set w=%1%
set p=ABCDEFGHIJKLMNOPQRSTUVWXYZ
set c=

for /l %%x in (1,1,%w%) do (
set /a r=!random! * 26 / 32768
call set c=!c!%%p:~!r!,1%%
)

echo %c%

endlocal


Yet, with 1st post in this thread, I thought is was kind of unique
that one could return an errorlevel as an ascii equivalent, although
I currently can't think of a use for the idea, its nevertheless
nice to know.


While I have everyone's attention, I do have some questions...


. Am I correct that 'call' expands all arguments passed?

Notice the fragment: "call set c=!c!%%p:~!r!,1%%"...
using only "set c=!c!%%p:~!r!,1%%", fails.

. Can delayedexpansion be written within a loop, or is this considered
undesirable?

. Are !exclamation marks! peculiar to only to the use of the
delayedexpansion directive?

. Is mixing two differing idioms like "!x!%y%" considered incorrect?


I appreciate any/all replies, most of my scripting experience lies
within the Unix realm...

Herbert Kleebauer

unread,
Apr 17, 2017, 10:51:50 AM4/17/17
to
On 17.04.2017 16:09, Mike Sanders wrote:

> if %1.==. (
> :: I need to test not only for the existence of %1,
> :: but ensure its numeric only, still working on this...
> exit /b 1
> )

@echo off
if [%1]==[] echo no parameter given &goto :eof
set /a n=%1
if not [%n%]==[%1] echo not a decimal number or leading zero &goto :eof

echo %n%

Mike Sanders

unread,
Apr 17, 2017, 11:34:13 AM4/17/17
to
Herbert Kleebauer <kl...@unibwm.de> wrote:

> @echo off
> if [%1]==[] echo no parameter given &goto :eof
> set /a n=%1
> if not [%n%]==[%1] echo not a decimal number or leading zero &goto :eof
>
> echo %n%

Using the above:

script.cmd 987k

Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

ver

Microsoft Windows [Version 10.0.14393]

Mike Sanders

unread,
Apr 17, 2017, 11:51:13 AM4/17/17
to
Mike Sanders <mi...@porkchop.bsd> wrote:

> if %1.==. (
> :: I need to test not only for the existence of %1,
> :: but ensure its numeric only, still working on this...
> exit /b 1
> )

solved (it appears):

@echo off

set w=%1

echo %w%| findstr /r "^[1-9][0-9]*$">nul

if not %errorlevel% equ 0 (
echo postive numaric input only
exit /b 1
)

note lack of space before pipe above '%w%|'...

<https://superuser.com/questions/404338/check-for-only-numerical-input-in-batch-file>

Herbert Kleebauer

unread,
Apr 17, 2017, 11:57:55 AM4/17/17
to
On 17.04.2017 17:31, Mike Sanders wrote:
> Herbert Kleebauer <kl...@unibwm.de> wrote:
>
>> @echo off
>> if [%1]==[] echo no parameter given &goto :eof
>> set /a n=%1
>> if not [%n%]==[%1] echo not a decimal number or leading zero &goto :eof
>>
>> echo %n%
>
> Using the above:
>
> script.cmd 987k
>
> Invalid number. Numeric constants are either decimal (17),
> hexadecimal (0x11), or octal (021).

set /a n=%1 2>nul

Mike Sanders

unread,
Apr 17, 2017, 12:30:11 PM4/17/17
to
Herbert Kleebauer <kl...@unibwm.de> wrote:

> set /a n=%1 2>nul

@echo off & setlocal enabledelayedexpansion

if [%1]==[] echo no parameter given & exit /b 1

set /a w=%1 2>nul

if not [%w%]==[%1] echo not a decimal number or leading zero & exit /b 1

set p=ABCDEFGHIJKLMNOPQRSTUVWXYZ
set c=

for /l %%x in (1,1,%w%) do (
set /a r=!random! * 26 / 32768
call set c=!c!%%p:~!r!,1%%
)

echo %c%

endlocal

exit /b 0

Mike Sanders

unread,
Apr 27, 2017, 10:12:17 PM4/27/17
to
@echo off

:: 'Anyone who attempts to generate random numbers
:: by deterministic means is, of course, living in
:: a state of sin' ~ John von Neumann
:: https://busybox.hypermart.net/index.html#cryptology

if [%~1]==[] goto :syntax

if %~1==/a (
set d=ABCDEFGHIJKLMNOPQRSTUVWXYZ
set /a n=26
) else if %~1==/n (
set d=1234567890
set /a n=10
) else (goto :syntax)

set /a e=(%random%*999)+999
set s=%date:~10,4%%date:~4,2%%date:~7,2%%e:~1,3%

echo. & echo %s% destory after use & echo.

setlocal enabledelayedexpansion

for /l %%x in (1,1,20) do (
for /l %%y in (1,1,5) do (
for /l %%z in (1,1,5) do (
set /a r=!random!*%n%/32768
call set c%%z=!c%%z!%%d:~!r!,1%%
)
)
echo !c1! !c2! !c3! !c4! !c5!
for /l %%c in (1,1,5) do set c%%c=
)

endlocal

exit /b

:syntax
cls
echo. & echo one time pad generater - Michael Sanders 2017
echo. & echo syntax: %~nx0 [/n^|/a] & echo.
echo /n numeric pad & echo /a alphabetic pad

Herbert Kleebauer

unread,
Apr 28, 2017, 3:22:06 PM4/28/17
to
On 28.04.2017 01:43, Mike Sanders wrote:

> for /l %%c in (1,1,5) do set c%%c=

You also should do that at the beginning of your batch because
you can't be sure that they are not already set.

Mike Sanders

unread,
Apr 29, 2017, 7:16:16 AM4/29/17
to
>> for /l %%c in (1,1,5) do set c%%c=
>
> You also should do that at the beginning of your batch because
> you can't be sure that they are not already set.

Doesnt setlocal constrain the scope though? Example...

@echo off

set c=global

setlocal
set c=local
echo %c%
endlocal

echo %c%

Mike Sanders

unread,
Apr 29, 2017, 7:39:00 AM4/29/17
to

setlocal enabledelayedexpansion

for /l %%x in (1,1,20) do (

rem c1, c2, c3... cleared at top of 1st loop
rem instead of bottom of 1st loop, so cleared
rem every time just to be safe...

for /l %%y in (1,1,5) do set c%%y=

for /l %%z in (1,1,5) do (
for /l %%p in (1,1,5) do (
set /a r=!random!*%n%/32768
call set c%%p=!c%%p!%%d:~!r!,1%%
)
)
echo !c1! !c2! !c3! !c4! !c5!
)

endlocal

Mike Sanders

unread,
Apr 29, 2017, 8:42:45 AM4/29/17
to
> ...from a project I'm working on...

Final version, unless someone spots something out of place.


windows ---------------------------------------------------


@echo off

:: one time pad generator...
::
:: https://en.wikipedia.org/wiki/One-time_pad
::
:: 'Anyone who attempts to generate random numbers
:: by deterministic means is, of course, living
:: in a state of sin' ~ John von Neumann
::
:: pad serialization...
::
:: .----------------------> year
:: | .---------------> month
:: | | .----------> day
:: [xxxx] [xx] [xx] [xxx]-> random 3 digit number
::
:: character count...
::
:: per cell: 005
:: per row: 025
:: per column: 100
:: per block: 500
::
:: entropy avg. (10 iterations)...
::
:: numeric: 3.31396
:: alphabet: 4.66583

if [%~1]==[] goto :syntax

if %~1==/n (
set d=0987654321
set /a n=10
) else if %~1==/a (
set d=ZYXWVUTSRQPONMLKJIHGFEDCBA
set /a n=26
) else (goto :syntax)

set /a e=(%random%*999)+999
set s=%date:~10,4%%date:~4,2%%date:~7,2%%e:~1,3%

echo. & echo %s% destroy after use & echo.

setlocal enabledelayedexpansion

for /l %%x in (1,1,20) do (

for /l %%y in (1,1,5) do set c%%y=

for /l %%z in (1,1,5) do (
for /l %%p in (1,1,5) do (
set /a r=!random!*%n%/32768
call set c%%p=!c%%p!%%d:~!r!,1%%
)
)

echo !c1! !c2! !c3! !c4! !c5!

)

endlocal

exit /b

:syntax
cls
echo. & echo one time pad generator - Michael Sanders 2017
echo. & echo syntax: %~nx0 [/n^|/a] & echo.
echo /n numeric pad & echo /a alphabetic pad


unix ------------------------------------------------------


#!/bin/sh

<<DOC

one time pad generator...

https://en.wikipedia.org/wiki/One-time_pad

'Anyone who attempts to generate random numbers
by deterministic means is, of course, living
in a state of sin' ~ John von Neumann

pad serialization...

.----------------------> year
| .---------------> month
| | .----------> day
[xxxx] [xx] [xx] [xxx]-> random 3 digit number

character count...

per cell: 005
per row: 025
per column: 100
per block: 500

entropy avg. (10 iterations)...

numeric: 3.31396
alphabet: 4.66583

DOC

syntax() {

clear

cat <<MSG

one time pad generator - Michael Sanders 2017

syntax: otp.sh [-n|-a]

-n numeric pad
-a alphabetic pad

MSG

exit 1

}

case $1 in
-n|-N) v=digit;;
-a|-A) v=upper;;
*) syntax;;
esac

r=$(strings /dev/urandom | tr -cd "[:$v:]" | fold -w 5 | head -n 100)

cat <<OTP

$(date +%Y%m%d)$(echo $r | cut -c 1-3) destroy after use

$(echo "$r" | awk '{printf("%s ", $0); if (NR % 5 == 0) printf("\n")}')

OTP

exit 0

# eof

Mike Sanders

unread,
Apr 29, 2017, 1:42:56 PM4/29/17
to
> Final version, unless someone spots something out of place.

<https://busybox.hypermart.net/one-time-pad.html>

And many thanks for your help Herbert, I appreciate it.

Herbert Kleebauer

unread,
Apr 29, 2017, 3:32:43 PM4/29/17
to
On 29.04.2017 19:39, Mike Sanders wrote:
>> Final version, unless someone spots something out of place.
>
> <https://busybox.hypermart.net/one-time-pad.html>

If this batch is not only for your use, you should
mention that this line depends on the local date format.

Mike Sanders

unread,
Apr 29, 2017, 7:37:24 PM4/29/17
to
Herbert Kleebauer <kl...@unibwm.de> wrote:

> If this batch is not only for your use, you should
> mention that this line depends on the local date format.

:: choose locale specific date format...

:: us: mm/dd/yyyy
::for /f "tokens=2-4 delims=/ " %%x in ('date /t') do set s=%%z%%x%%y

:: ? uk: dd/mm/yyyy
::for /f "tokens=1-3 delims=/" %%x in ('date /t') do set s=%%z%%y%%x

:: ? de: mm.dd.yyyy
for /f "tokens=1-3 delims=." %%x in ('date /t') do set s=%%z%%x%%y

set /a e=(%random%*999)+999

echo. & echo %s%%e:~1,3% destroy after use & echo.

Herbert Kleebauer

unread,
Apr 30, 2017, 3:04:09 AM4/30/17
to
On 30.04.2017 01:34, Mike Sanders wrote:

> :: ? de: mm.dd.yyyy
> for /f "tokens=1-3 delims=." %%x in ('date /t') do set s=%%z%%x%%y

D:\>for /f "tokens=1-3 delims=." %x in ('date /t') do echo set s=%z%x%y
set s=2017 3004

D:\>for /f "tokens=1-3 delims=. " %x in ('date /t') do echo set s=%z%x%y
set s=20173004

Mike Sanders

unread,
Apr 30, 2017, 10:30:29 AM4/30/17
to
Herbert Kleebauer <kl...@unibwm.de> wrote:

> D:\>for /f "tokens=1-3 delims=." %x in ('date /t') do echo set s=%z%x%y
> set s=2017 3004
>
> D:\>for /f "tokens=1-3 delims=. " %x in ('date /t') do echo set s=%z%x%y
> set s=20173004

.----------------------> year
| .---------------> month
| | .----------> day
[xxxx] [xx] [xx] [xxx]-> random 3 digit number

de default: dd.mm.yyyy



for /f "tokens=1-3 delims=. " %i in ('date /t') do set s=%k%j%i
set /a e=(%random%*999)+999
echo %s%%e:~1,3%

Mike Sanders

unread,
May 1, 2017, 10:26:59 AM5/1/17
to
Alright, this is done (no news is good news). The page is updated & ready.

<https://busybox.hypermart.net/one-time-pad.html>

Earnest thanks Herbert, I learned allot & hope to learn more in time.
0 new messages