Page 1 of 1

random variables

Posted: 19 Jan 2011 20:47
by scienceguru1.bat
i can do the %random% command, and i can set color manually, but how do you set the # of didgts in the %random% command, and how can you set random color?

Re: random variables

Posted: 20 Jan 2011 12:36
by aGerman
It's simple. The magic word is modulo.

Have a look at this snippet:

Code: Select all

@echo off &setlocal

:: all possible HEX digits for background and foreground color
set "digits=0123456789ABCDEF"


:loop
:: calculate two random numbers between 0 and 15 using modulo operation
set /a n1 = %random% %% 16 , n2 = %random% %% 16

:: background and foreground color should never be the same, otherwise calculate new numbers
if %n1%==%n2% goto loop

:: change the colors by picking up two single digits depending on the random numbers
setlocal enabledelayedexpansion
color !digits:~%n1%,1!!digits:~%n2%,1!
endlocal

pause

Regards
aGerman

Re: random variables

Posted: 20 Jan 2011 15:55
by scienceguru1.bat
Thanks, it works