0

I'm aware that set zeroThroughNine=%Random%*9/32768 followed by echo %zeroThroughNine% will produce a a random number between and including 0 and 9. But it seems the interpreter doesn't evaluate the contents of the variable every time it is called, and as such, echo %zeroThroughNine% produces, for example, 7 every time.

I looked up a method for running commands using variables so that I could try to force it to work. I liked the question because it was very basal in its approach; something along the lines of "How can I run commands using variables?", tagged appropriately. I didn't much care for the answer because it was very narrow. The highest voted and selected answer was:

Simple. Just run set commandVar=echo "Hello world.", followed by echo %commandVar%.

Of course the truth is that only works for the echo command. >: [

Anyway I'll stop complaining. This is what I've tried:

set zeroThroughNine=set /a number=%Random%*9/32768 & echo %number% followed by echo %zeroThroughNine%

Unfortunately the & echo %number% section of my SET command runs immediately, producing "%number%" as output --and using echo %zeroThroughNine% produces "set /a number=8436*9/32768", for example, as output.

So two questions: How can I universally achieve running commands with the use of variables (or some alternative method), and perhaps more pressing, how can I achieve producing a new random number at the command line with each new command calling?

Musixauce3000
  • 549
  • 1
  • 3
  • 11

3 Answers3

1

You should set number before you set zeroThroughNine to the command, like so:

set /a number=%Random%*9/32768
set zeroThroughNine=echo %number%
%zeroThroughNine%

Also, since zeroThroughNine already is an echo command, you don't need to add the extra echo before it.

EDIT:

Taking into account your Random calculation is needlessly complicated, the final code should be something like this (1 - 10 exclusive):

set /a number=%Random% %% 10
set zeroThroughNine=echo %number%
%zeroThroughNine%

Important thing is, rather than trying to do it all on one line, it is much more readable by separating it into two.

coltonb
  • 66
  • 7
  • What's not working? I'd be happy to take a look at how you're using this in context, as using my snippit does work. – coltonb Dec 10 '15 at 20:53
  • [http://i.imgur.com/mig6RqE.png] Both of them produce the same number repeatedly. Yesterday at the end of my shift is when I told you it was working. I was just tired lol. I watched it produce the same number repeatedly and for some reason I was satisfied --one of those moments like when you try to put your cellphone in the fridge :p – Musixauce3000 Dec 11 '15 at 13:54
0

The CALL SET syntax allows a variable substring to be evaluated, the CALL page has more detail on this technique, in most cases a better approach is to use Setlocal EnableDelayedExpansion.

Command line (note that all % percent signs are escaped as ^% and that > and & characters are escaped within a pair of " double quotes:

set "zeroThroughNine=call set /a number=^%Random^% ^% 10>nul & call echo number=^%number^%"
%zeroThroughNine%
for /L %G in (1, 1, 10) do @%zeroThroughNine%

Batch script, CALL method (note that all % percent signs are escaped as %%):

@echo OFF
SETLOCAL
set "_zeroThroughNine=call set /a _number=%%Random%% %%%% 10 & call echo number=%%_number%%"
echo check variables 
set _
echo output
%_zeroThroughNine%
for /L %%G in (1,1,10) do %_zeroThroughNine%
echo check variables after evaluating 
set _
ENDLOCAL

Batch script, EnableDelayedExpansion only for output:

@echo OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set "_zeroThroughNine=set /a _number=!Random! %% 10 & echo Number=!_number!"

SETLOCAL EnableDelayedExpansion
echo check variables 
set _
echo output
%_zeroThroughNine%
for /L %%G in (1,1,10) do %_zeroThroughNine%
echo check variables after evaluating 
set _
ENDLOCAL
ENDLOCAL

Batch script, EnableDelayedExpansion script wide (note that ! exclamation sign is escaped as ^!):

@echo OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
set "_zeroThroughNine=set /a _number=^!Random^! %% 10 & echo NUMBER=^!_number^!"
echo check variables 
set _
echo output
%_zeroThroughNine%
for /L %%G in (1,1,10) do %_zeroThroughNine%
echo check variables after evaluating 
set _
ENDLOCAL
JosefZ
  • 28,460
  • 5
  • 44
  • 83
-1

Check out this question. Basically, it's an issue with how the %random% environment variable works...

EDIT:

To elaborate, the reason your random value was always 7 is because of how cmd's pseudo-random number generator works, not because of how the variables are interpreted. The matter is explained very well in this answer.

Essentially, in repeated runs of a batch file, %RANDOM% will produce a value very close to the previous run. Thus, the expression %RANDOM%*9/32768 produces the same result in each separate run because of the random value.

If I understand correctly, the question you're asking is how to better generate a random value 0 - 9 inclusive, which would be by using the following expression:

set /a zeroThroughNine=%RANDOM% %% 10
Community
  • 1
  • 1
Tommy Harris
  • 109
  • 6
  • That isn't an answer, you only mention that his question is a duplicate. Which is kind of nice because you did the searching he should have done – Marged Dec 10 '15 at 20:06
  • Good point, though the answers in that question explain why his random was 7 every time. I can't agree with you that it's not an answer, but maybe an incomplete one. Perhaps that should've been a comment? Forgive me, I'm new here. – Tommy Harris Dec 10 '15 at 22:41
  • Tommy, I have a "matrix" bat file that adjust the properties of command prompt to thematically resemble The Matrix. In this bat file is a goto loop at triggers a fresh calculation of 96 unique variables containing the %random% environment variable and my arithmetic --followed by an echo command that calls them all one after another. The result is a repeatedly generated output line of 96 single digit numbers ranging from 0 to 9.The resulting effect is vaguely reminiscent of the matrix. – Musixauce3000 Dec 11 '15 at 15:53
  • Trouble is, in order for this to work I've had to manual write out each variable. But that's beside the point. Each of the ninety six variables when called to be rewritten by the goto loop create satisfactorily randomized numbers. I just want to know how to do the same thing at the command line – Musixauce3000 Dec 11 '15 at 15:55