CMD %random% Variable Controller

by NotePro in Circuits > Software

358 Views, 3 Favorites, 0 Comments

CMD %random% Variable Controller

both dot bat pic.PNG

I have made a %random% variable controller that always outputs a number 1-5. This was made because I was trying to make a game that required an element of randomness in it and so I made up these two programs to help me.

Supplies

notepadpic.PNG

For this project, you will need Notepad (AKA the best app ever), and preferably some basic knowledge of the coding language. (As this program might not be useful if you don't know how to use it.)

There are no steps. Only need to Copy and Paste the work into Notepad and name the file anything_you_want.bat


Warning!! If someone asks you to put code you don't understand on a computer, DONT! Fun fact, You don't need to let CMD have administrator permissions for it to harm your computer. I have lost my cursor in the Wireless USB connector for my mouse from making my computer restart too many times in the same second.

Copy and Paste

another dot bat pic.PNG

This is the Beta program and has slow speeds since the %random% variable can produce a large number and the loop, while quite fast, only takes 5 away each time. Copy the code between the **** To skip to the latest version of the code, go to the next step.

*****************************************************************************************

@echo off

title random function controller

:top

cls

echo this version allows you to get a number 1-5 ALWAYS!! :)

pause

set r=%random%

:loop

set /a r= %r% -5

if %r% LSS 6 goto num

goto loop

:num

echo the number is %r%

pause

:StartAgain

cls

echo again??

pause

set r=%random%

goto loop

****************************************************************************************

Version 2.0

a dot bat pic.PNG

This is the faster improved version. Just copy and paste it into Notepad!

copy in between the ***********

*********************************************************************************

@echo off

title random function controller

:top

cls

echo this version allows you to quickly get a number 1-5 :)

pause

set r=%random%

:loop

rem // the subtraction part is -995 instead of -1000 because %r%-5 comes after it. It would be possible to get a negative number.

rem // I have the code "if %r% LSS 6 goto num" twice in the loop because %random% spits out a number between 0-32767. If the number that %random% retrieves is 5 or less, you could end up with a negative number.

if %r% LSS 6 goto num

if %r% GTR 1000 set /a r=%r% -995 

set /a r= %r% -5

if %r% LSS 6 goto num

goto loop

rem // instead of everything below, you would have your if statements.

:num

echo the number is %r%

pause

:StartAgain

cls

echo again??

pause

set r=%random%

goto loop

****************************************************************************************

Thank you for reading!

It would be great if you could explain some of your ideas for this program in the comments.

Final Thoughts

The %random% command is a special command. It outputs a number 0 through 32767

I have recently figured out that the most efficient way to do what I have shown in my instructable is to do

set /a var = %random% * (%highest-Value% - %lowest-Value%) / 32768 + %lowest-value%

Just set the variables to the numbers that you would prefer!