0

I’m having trouble with the %RANDOM% environment variable in the following command:

FOR /l %%A in (0,1,30) do set /a results=1600 + %RANDOM% %% (1900 - 1600 + 1) && echo %%A--!results!

I expect this output:

0--1656
1--1743
2--1629
3--1887
…

But I’m getting this:

0--1656
1--1656
2--1656
3--1656
…

The %RANDOM% variable is supposed to return a random number, but it’s giving the same number. What’s the problem and how can I fix it?

Synetech
  • 69,547
hbelouf
  • 781

1 Answers1

1

You need to use delayed expansion for the RANDOM variable as well:

FOR /l %%A in (0,1,30) do set /a results=1600 + !RANDOM! %% (1900 - 1600 + 1) && echo %%A--!results!

Screenshot of command-prompt with expected results from script

Synetech
  • 69,547