Answer» I don't know if this can e done in batch but can anyone randomize a variables value(in batch)? Like for example I give these 3 options and the variable gets as value anyone of them. for example:
value1 - echo Hello value2 - echo World value3 - echo "
Now, each time i execute the batch file i want this variable not to have the same value always. If that can be done I'd need some help.sure can be done
Code: [Select]@echo off
set r=%RANDOM% echo Random Number=%r%
set/a a=%random% %% 3 + 1 echo a=%a%
if %a%==1 echo Hello if %a%==2 echo World if %a%==3 echo " Explanation:
USING %random% generates a random number between 0 and 32767.
To restrict a random number to a range from 0 to N-1 you use set /a %random% modulus N. The modulus operator is a percent SIGN %. The modulus is the remainder that results from performing integer division. Thus %random% % 10 results in a random number whose lowest value could be 0 and whose highest value could be 9. To shift the range to be from 1 to N you add 1.
Modulus operator - NOTE that in a batch SCRIPT, (as opposed to on the command-line), you need to double up the % to %%.
Thus to generate a random number from 1 to 3 you could write
set /a rnum=%random% % 3 + 1 at the command prompt
and
set /a rnum=%random% %% 3 + 1 in a batch file.
|