Create a random Hex Generator

Microsoft Windows xp professional editio...
October 7, 2009 at 09:04:20
Specs: Windows 7
I am wanting to create a batch file that will randomly generate a Hex number that can be used to randomly change the color.

I have researched and can not find a way to generate anything other than a decimal number ( sorry to say I am just learning batch files ) here is what I have so far ( again only a decimal random )

setlocal
set /a r1=%random% %% 16
color %r1%


See More: Create a random Hex Generator

Report •


#1
October 7, 2009 at 11:51:21
I have since created a longer version ..... however if anyone knows a shorter method I am willing to hear it .... this is what I came up with.

@echo off
setlocal
set /a r1=%random% %% 16
:1color =first Hex color character
IF %r1%==0 (
set r10=0
) ELSE (
goto 1color11
)
:1color11
IF %r1%==1 (
Set r10=1
) ELSE (
goto 1color12
)
:1color12
IF %r1%==2 (
set r10=2
) ELSE (
goto 1color13
)
:1color13
IF %r1%==3 (
set r10=3
) ELSE (
goto 1color14
)
:1color14
IF %r1%==4 (
set r10=4
) ELSE (
goto 1color15
)
:1color15
IF %r1%==5 (
set r10=5
) ELSE (
goto 1color16
)
:1color16
IF %r1%==6 (
set r10=6
) ELSE (
goto 1color17
)
:1color17
IF %r1%==7 (
set r10=7
) ELSE (
goto 1color18
)
:1color18
IF %r1%==8 (
set r10=8
) ELSE (
goto 1color19
)
:1color19
IF %r1%==9 (
set r10=9
) ELSE (
goto 1color20
)
:1color20
IF %r1%==10 (
set r10=a
) ELSE (
goto 1color21
)
:1color21
IF %r1%==11 (
set r10=b
) ELSE (
goto 1color22
)
:1color22
IF %r1%==12 (
set r10=c
) ELSE (
goto 1color23
)
:1color23
IF %r1%==13 (
set r10=d
) ELSE (
goto 1color24
)
:1color24
IF %r1%==14 (
set r10=e
) ELSE (
goto 1color25
)
:1color25
IF %r1%==15 (
set r10=f
) ELSE (
goto 2color10
)

:2color=first Hex color character
:2color10
set /a r2=%random% %% 16
IF %r2%==0 (
set r20=0
) ELSE (
goto 2color11
)
:2color11
IF %r2%==1 (
Set r20=1
) ELSE (
goto 2color12
)
:2color12
IF %r2%==2 (
set r20=2
) ELSE (
goto 2color13
)
:2color13
IF %r2%==3 (
set r20=3
) ELSE (
goto 2color14
)
:2color14
IF %r2%==4 (
set r20=4
) ELSE (
goto 2color15
)
:2color15
IF %r2%==5 (
set r20=5
) ELSE (
goto 2color16
)
:2color16
IF %r2%==6 (
set r20=6
) ELSE (
goto 2color17
)
:2color17
IF %r2%==7 (
set r20=7
) ELSE (
goto 2color18
)
:2color18
IF %r2%==8 (
set r20=8
) ELSE (
goto 2color19
)
:2color19
IF %r2%==9 (
set r20=9
) ELSE (
goto 2color20
)
:2color20
IF %r2%==10 (
set r20=a
) ELSE (
goto 2color21
)
:2color21
IF %r2%==11 (
set r20=b
) ELSE (
goto 2color22
)
:2color22
IF %r2%==12 (
set r20=c
) ELSE (
goto 2color23
)
:2color23
IF %r2%==13 (
set r20=d
) ELSE (
goto 2color24
)
:2color24
IF %r2%==14 (
set r20=e
) ELSE (
goto 2color25
)
:2color25
IF %r2%==15 (
set r20=f
) ELSE (
goto randomcheck
)
:randomcheck
If %r1%==%r2% (
goto 1color
) ELSE (
goto line99
)
:line99
call set r30=%%r10%%%r20%%
color %r30%


Report •

#2
October 7, 2009 at 17:11:30
Here is a different way.....

@ECHO OFF
setlocal
for /l %%a in (0 1 9) do set /%%a=%%a
for %%a in ("/10=a" "/11=b" "/12=c" "/13=d" "/14=e" "/15=f") do set %%~a
:loop
set /a r1=%random% %% 16
call set r1=%%/%r1%%%
set /a r2=%random% %% 16
call set r2=%%/%r2%%%
if %r1%==%r2% goto loop
color %r1%%r2%
echo %r1%
echo %r2%
pause

[edit]
A better way(no intermediate variables)...

@ECHO OFF
setlocal
:loop
set /a r1=%random% %% 16
set /a r2=%random% %% 16
for %%a in ("10=a" "11=b" "12=c" "13=d" "14=e" "15=f") do (
    call set r1=%%r1:%%~a%%
    call set r2=%%r2:%%~a%%
)
if %r1%==%r2% goto loop
color %r1%%r2%
echo %r1%
echo %r2%
pause
goto loop


Report •

#3
October 8, 2009 at 09:06:15
Hi Judago,

Why call set?


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
October 8, 2009 at 13:47:51
Hi M2,

I'm not sure if your asking why I choose call over using delayed expansion or what exactly it does.

No real reason to use call over over delayed expansion here(maybe just a subconscious character count thing....). Using set normally looked promising, except in reality it was throwing back the invalid batch parameter error.....

If it the later, which I doubt because your such a pro at this, it's basically how everyone got along before delayed expansion (dos, 9x, nt4). The "nested variable" section of my how to (link in sig) explains it.


Batch Variable how to


Report •

#5
October 8, 2009 at 13:59:56
Hi Judago,

Thanks. I'll have to think about this for a while.


=====================================
Helping others achieve escape felicity

M2


Report •


Ask Question