random variables
Moderator: DosItHelp
-
- Posts: 44
- Joined: 01 Jan 2011 20:54
random variables
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
It's simple. The magic word is modulo.
Have a look at this snippet:
Regards
aGerman
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