Sean McLain
Sean McLain

Reputation: 437

Set a variable to be another variable with a number + or -

I am writing a program in batch. It has to take a variable which is a number and add or subtract from that variable to get another number.

I currently have:

set /A str="%RANDOM% %% 6"+1+"%RANDOM% %% 6"+1+"%RANDOM% %% 6"+3
set /A dex="%RANDOM% %% 6"+1+"%RANDOM% %% 6"+1+"%RANDOM% %% 6"+3
set /A con="%RANDOM% %% 6"+1+"%RANDOM% %% 6"+1+"%RANDOM% %% 6"+3
set /A int="%RANDOM% %% 6"+1+"%RANDOM% %% 6"+1+"%RANDOM% %% 6"+3
set /A wis="%RANDOM% %% 6"+1+"%RANDOM% %% 6"+1+"%RANDOM% %% 6"+3
set /A cha="%RANDOM% %% 6"+1+"%RANDOM% %% 6"+1+"%RANDOM% %% 6"+3 

set /a Data11="%str% %% 1"+2
set /a Data12=%dex%
set /a Data13="%con% %% 1"+2
set /a Data14="%int% %% 1"-2
set /a Data15=%wis%
set /a Data16=%cha%

Unfortunately it is currently setting the Data11, 13, and 14 to just the +2 or -2.

I want it to work this way so I can have many different sets of alterations to preform on the str, dex, ectra variables which in turn depend on user input and choice.

How can I get it to work as intended?

Upvotes: 0

Views: 60

Answers (3)

foxidrive
foxidrive

Reputation: 41234

You don't need quotes for most arithmetic or percent signs on normal variables
- and I removed the %% 1 for this example.

set /A str=%RANDOM% %% 6+1+%RANDOM% %% 6+1+%RANDOM% %% 6+3
set /A dex=%RANDOM% %% 6+1+%RANDOM% %% 6+1+%RANDOM% %% 6+3
set /A con=%RANDOM% %% 6+1+%RANDOM% %% 6+1+%RANDOM% %% 6+3
set /A int=%RANDOM% %% 6+1+%RANDOM% %% 6+1+%RANDOM% %% 6+3
set /A wis=%RANDOM% %% 6+1+%RANDOM% %% 6+1+%RANDOM% %% 6+3
set /A cha=%RANDOM% %% 6+1+%RANDOM% %% 6+1+%RANDOM% %% 6+3 

set /a Data11=str + 2
set /a Data12=dex
set /a Data13=con + 2
set /a Data14=int - 2
set /a Data15=wis
set /a Data16=cha

set data
pause

Upvotes: 1

Thejaka Maldeniya
Thejaka Maldeniya

Reputation: 1076

Note that the following are also valid:

SET/A Data11="str %% 2 + 2"
SET/A "Data11=str %% 2 + 2"
SET/A Data12=dex

SET/A temp=dex+1

More information and examples:

Microsoft

SS64.com

Upvotes: 0

Ofir Luzon
Ofir Luzon

Reputation: 10907

you set Data11 variables to be str modulus 1 which is 0

Upvotes: 2

Related Questions