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
Related
Menu("RADIAN CONVERSION","DEGREE TO RADIAN",1,"RADIAN TO DEGREE",2
Lbl 1
Input "DEGREE=",C
C/180Frac→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
I have run the ‘More Bouncing Balls’ Basic program from chapter 5 of the C64 user’s manual, with the addition from the final page of the chapter. The code is as follows:
10 PRINT CHR$(147):REM SHIFT CLR/HOME
20 POKE 53280,7 : POKE 53281,13
30 X=1:Y=1
40 DX=1:DY=1
50 POKE 1024 + X + 40*Y, 81
60 FOR T=1 TO 10: NEXT T
70 POKE 1024 + X + 40*Y, 32
80 X=X+DX
90 IF X=0 OR X=39 THEN DX=-DX
100 Y=Y+DY
110 IF Y=0 OR Y=24 THEN DY=-DY
120 GOTO 50
To this were added the lines at the end, ɔ:
21 FOR L=1 TO 10
25 POKE 1024+INT(RND(1)*1000),160
27 NEXT L
85 IF PEEK(1024+X+40*Y)=160 THEN DX=-DX:GOTO 80
105 IF PEEK(1024+X+40*Y)=160 THEN DY=-DY:GOTO 100
These lines are not relevant to the question, but I included them for sake of completeness.
I wanted to add randomness to the direction of the ball (chr$(81)), and noticed that by changing DX and DY to other numbers, I would get it to move at angles other than 45°; of course, having both DX and DY set to 1, would have them both ‘push’ the ball in perpendicularly opposite directions, ɔ: halfway between both, equalling 45°.
But when trying to use a random number, I would get truly odd behaviour. I assumed the number had to be between 0 and 1, and so tried (INT(10*RND(1))+1)/10, and changed line 40 to set DX and DY to this random number. What I got instead was some very odd behaviour. The ball would move very fast at a predictable angle, disappearing at the right side and reappearing on the left, moving a few lines down, then disappearing completely, then turning up on top of the screen drawing unmoving balls one after another horizontally, then crash.
When instead setting DX or DY to an integer, i.e. 2, I would still get some strange behaviour, such as the ball disappearing at one end and reappearing at the opposite, and on this occasion the program would end after a few seconds.
What is causing this erratic behaviour? And how can I set the parameters in line 40 to allow the ball to move in different directions (kind of like in Pong) when it hits a wall?
Note 1: When changing DX and DY in lines 80 and 100 instead, I got some interesting jittering movement, but as expected, as though the ball drawn on-screen was an uneven sphere.
Note 2: I am aware one generally should not include tags in titles, but was unsure whether the question would be too unsearchable if I left them out. Feel free to edit the title if need be; I am happy to be educated.
I modified the program in this way:
1-DX is step for X.
2-DY is step for Y.
2-VX is direction of X, -1 left and +1 rigth.
3-XY is direction of Y, -1 up and +1 down.
3-When Bouncing Ball angle changes randomly (subroutine 300)
Calculation of DX and DY is for a right triangle with hypotenuse of 1 (one).
4-When plotting use only integer numbers so the "ball" doesn't have odd moves.
5-Control off limits, so "ball" doesn't disappear.
5 rem 2018-08-24 bouncing balls
6 rem https://stackoverflow.com/questions/51907035
7 rem /generating-random-direction-in-c64-basic
10 print chr$(147);:rem shift+clr/home=clear screen
20 poke 53280,7:poke 53281,13
25 rem random initial position
40 p=rnd(1)*40:x=p
45 q=rnd(1)*25:y=q
50 gosub 300
60 rem vector direction
70 vx=(rnd(1)<0.5):if vx>=0 then vx=1
80 vy=(rnd(1)<0.5):if vy>=0 then vy=1
100 rem plot
110 poke 1024+int(p)+40*int(q),32
120 poke 1024+int(x)+40*int(y),81
130 for t=1 to 30:next t
140 p=x:q=y
150 x=x+dx*vx
160 ca=0:rem change angle
170 if x<=0 or x>=39 then vx=-vx:ca=-1
175 if x<0 then x=0
176 if x>39 then x=39
180 y=y+dy*vy
190 if y<=0 or y>=24 then vy=-vy:ca=-1
195 if y<0 then y=0
196 if y>24 then y=24
200 if ca then gosub 300
210 goto 100
300 rem random angle between 15 and 75 d
egrees
305 rem a=angle in degrees r=radians
310 a=15+rnd(1)*(75-15+1):r=a*{pi}/180
320 dx=cos(r)
330 dy=sin(r)
340 return
On C64 replace {pi} using SHIFT+UP_ARROW.
If line 110 is REM then you can see the walk.
I modified the program so:
- Start position X and Y are random
- Direction DX and DY are random, values -1 or +1
10 PRINT CHR$(147):REM SHIFT CLR/HOME
20 POKE 53280,7:POKE 53281,13
25 REM RANDOM INITIAL POSITION
30 X=INT(RND(1)*39)+1:Y=INT(RND(1)*24)+1
35 REM RANDOM DIRECTION
40 DX=(RND(1)<0.5):IF DX>=0 THEN DX=1
45 DY=(RND(1)<0.5):IF DY>=0 THEN DY=1
50 POKE 1024+X+40*Y,81
60 FOR T=1 TO 30:NEXT T
70 POKE 1024+X+40*Y,32
80 X=X+DX
90 IF X<=0 OR X>=39 THEN DX=-DX
100 Y=Y+DY
110 IF Y<=0 OR Y>=24 THEN DY=-DY
120 GOTO 50
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.
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.
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.