Computing.Net > Forums > Programming > Create a random Hex Generator

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Create a random Hex Generator

Reply to Message Icon

Name: sylvirknight
Date: October 7, 2009 at 09:04:20 Pacific
OS: Windows 7
Product: Microsoft Windows xp professional edition
Subcategory: Batch
Tags: batch, hex, random, generate
Comment:

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%



Sponsored Link
Ads by Google

Response Number 1
Name: sylvirknight
Date: October 7, 2009 at 11:51:21 Pacific
Reply:

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%


0

Response Number 2
Name: Judago
Date: October 7, 2009 at 17:11:30 Pacific
Reply:

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


0

Response Number 3
Name: Mechanix2Go
Date: October 8, 2009 at 09:06:15 Pacific
Reply:

Hi Judago,

Why call set?


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

M2


0

Response Number 4
Name: Judago
Date: October 8, 2009 at 13:47:51 Pacific
Reply:

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


0

Response Number 5
Name: Mechanix2Go
Date: October 8, 2009 at 13:59:56 Pacific
Reply:

Hi Judago,

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


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

M2


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Create a random Hex Generator

Random Number Generator www.computing.net/answers/programming/random-number-generator/5959.html

Random number generator www.computing.net/answers/programming/random-number-generator/12990.html

random number with probability www.computing.net/answers/programming/random-number-with-probability/8217.html