calculating the average of 10 for loop results in batch - windows

so i've created a for loop that calculates (x mod 6) + 2 for every number from 1-10 (inclusive), with "x" representing each individual number as the loop increments. however, i'd like to calculate the average for each of the 10 results, but i actually have no idea as to how i'd format the loop to do so. i'd rather not do something like echo "1 + 2 + 3 + etc" as that seems extremely lazy and ill-constructed. i feel like this is something relatively easy, but i've tried searching it up and attempting another loop, but to no avail. any and all help is much appreciated!
here's the loop:
:ForLoop
#echo off &setlocal enabledelayedexpansion
echo This loop calculates the results for (x mod 6) + 2 with x being each number from1-10 (inclusive).
echo.
for /L %%x in (1, 1, 10) do (
SET /a VAR=%%x %% 6+2
set /a CAL=!VAR! %% 8
echo %%x MOD 6 + 2 = !CAL!
)

#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:ForLoop
echo This loop calculates the results for (x mod 6) + 2 with x being each number from1-10 (inclusive).
echo.
SET /a iterations=10
SET /a sum=0
for /L %%x in (1, 1, %iterations%) do (
SET /a VAR=%%x %% 6+2
SET /a sum+=var
SET /a avg=sum / %%x
echo %%x MOD 6 + 2 = !var! average so far=!avg! (from !sum! / %%x^)
)
GOTO :EOF
There's no need to do the %% 8 jazz - since var can only be (0..5)+2, this gives a range of 2..7.
set /a uses the run-time value of the variables, so !var! is not required - %var% would use the initial value of var.
Note that the average is truncated as batch does integer mathematics.

Related

I get a sytax error at line 4 (I knwo there excess lines rn)

Menu("RADIAN CONVERSION","DEGREE TO RADIAN",1,"RADIAN TO DEGREE",2
Lbl 1
Input "DEGREE=",C
C/180Frac→W
Goto 3
Lbl 3
Goto 4
Lbl 2
Input "RADIAN=""LEAVE OUT PIE",A
A(180)→D
Goto 5
Lbl 4
Disp "RADIAN",P
Lbl 5
Disp "DEGREE=",D
Stop
Error is in line 4 the question mark is just the frac conversion
I know there is easier ways to do this but i just wanna learn

Generating two random coordinates in windows batch file

I intended to output the following in a Windows batch file to print five random coordinates wihtin a given range. I know this task is easier on other languages but I'm forced to use Windows batch programming.
INTENDED OUTPUT:
x1:1344
y1:1995
x1:1347
y1:1998
x1:1350
y1:1996
x1:1345
y1:1999
x1:1345
y1:1995
)
My windows batch coding is not that great and I complied the following using Google Fu.
I'm not sure what I'm missing but the following code is supposed to output two random numbers as a coordinate (x1 and y1).
However my code below does not give me my intended output
CODE
#echo off & setlocal EnableDelayedExpansion
for /L %%a in (1 1 5) do (
REM Random X coordinate between 1336 1350)
call:rand 1336 1350
set /A x1= !RAND_NUM!
echo(!x1!)
REM Random Y coordinate between 1950 2000)
call:rand 1950 2000
set /A y1= !RAND_NUM!
echo(!y1!)
)
goto:EOF
:rand
SET /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1
goto:EOF
My output is missing the five y coordinates and prints the first five x coordinates first and then one y coordinate and stops.
OUTPUT:
x1:1344
x1:1349
x1:1347
x1:1342
x1:1347
y1:1995)
The closing parenthesis ) inside of the code block is seen as the closing parenthesis of the code block itself, if you really needed them, which you don't you would need to escape them ^) but instead remove them entirely. Also no need to set /A x1 and y1 just set can work on its own, but even that set is not needed:
#echo off & setlocal EnableDelayedExpansion
for /L %%a in (1 1 5) do (
REM Random X coordinate between 1336 1350
call:rand 1336 1350
echo(x1:!RAND_NUM!
REM Random Y coordinate between 1950 2000
call:rand 1950 2000
echo(y1:!RAND_NUM!
)
goto:EOF
:rand
set /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1
goto :EOF

How to generate random Hex String from batch file?

Friends, I am trying to generate a random hex string on my web server. I am using Apache HTTPD on Windows 10. I am using the hex string for session ID's etc.
The code I have tried so far:
setlocal enabledelayedexpansion
set HESSTR=0123456789ABCDEF
set /a tempone="("%RANDOM%"*"16")"
set /a RANHEXS=tempone"/"32767
echo %RANHEXS%
set hexout=!HESSTR:~%RANHEXS%,1!
echo %hexout%
pause
endlocal
But this code Always returns 7... What am I doing wrong?
Thanks a lot!
Here is a method to create a random hexadecimal digit that relies on the undocumented built-in environment variable =ExitCode, which returns the exit code as an 8-digit hexadecimal number:
rem // Set ErrorLevel and exit code to a random number:
cmd /C exit %RANDOM%
rem // Return the last digit of the hexadecimal exit code:
echo %=ExitCode:~-1%
Note that the exit code can reach from 00000000 to FFFFFFFF. The value of RANDOM however covers the range from 0 to 32767, which is 0000 to 7FFF expressed in hexadecimal notation; so you can build a 3-digit hexadecimal number at most by using the last three digits.
#Set /a num=%random% %% 16 + 1
Echo %num% / 16
See set /?. % is defined in the C language as
% The result of the remainder operator is the remainder when the first operand is divided by the second
And we need to escape the % with another %, so %%.
So you divide a number by the range you want - so 165 %% 16 = 5 (the remainder). Then we add 1 to make it 1 to 16 else it would be 0 to 15.
PS CMD is unusual is being 0-32767, most random numbers are between 0 and 1 then you multiply. This is from VBScript's help - Int((upperbound - lowerbound + 1) * Rnd + lowerbound) so (16 - 1 + 1) * RandomNum + 1.

Bash csh Loop through 2 list

In csh or any other mac friendly script language, I would like to run some form of a loop that reads through 2 lists. Need a bit of help with the syntax.
For example:
variable1 = (1, 2, 3, 4, ...)
variable2 = (one, two, three, four, ...)
Foreach number ($variable1)
echo "{$variable1} equals {$variable2}"
end
When I try, the second variable2 is not recognized within the loop.
#!/bin/bash
variable1=(1 2 3 4)
variable2=(one two three four)
for ((i=0;i<${#variable1[#]};i++)); do
echo "${variable1[$i]} equals ${variable2[$i]}"
done
Output:
1 equals one
2 equals two
3 equals three
4 equals four
Please try this:
#! /bin/csh
set variable1 = (1 2 3 4)
set variable2 = (one two three four)
set i = 1
foreach x ( $variable1 )
echo "$x equals $variable2[$i]"
# i = $i + 1
end
The result is:
1 equals one
2 equals two
3 equals three
4 equals four

ampersand used in SET /A command in Windows batch script

Could you please advice what using "&" or "^" in SET command means (I haven't found any explanation by using Google).
For example, following Windows batch code block
SET V_COMMAND=3
SET /A V_FLAG="%V_COMMAND%&2"
echo VFlag is: %V_FLAG%
produces:
VFlag is: 2
But I haven't any opinion about what command above does.
Also there is another case with "^":
SET V_COMMAND=3
SET /A V_FLAG="%V_COMMAND%^3"
echo VFlag is: %V_FLAG%
For this case output is:
VFlag is: 0
Since you are using set /a, the indicated characters are bitwise operators:
& = bitwise AND = 1 if both bits are 1
^ = bitwise XOR = 1 if only one of the two bits is 1
So if a is 10 (1010 in binary) and b is 13 (1101 in binary)
1010 1010
1101 1101
---- ----
1000 = a & b 0111 = a ^ b
Or in your case with 3 dec = 11 bin and 2 dec = 10 bin
11 11
10 11
-- --
10 = 3&2 = 2 00 = 3^3 = 0
Those are bitwise operators - & is bitwise AND, and ^ is bitwise XOR. These bitwise operators are only available with SET /A.

Resources