0

So I've been trying to make the following code:

set /a num1=10
set /a num2=%random% %%60 +%num1%
echo %num2%

(This is simplified)

For this code I need the +%num1% to be a variable because I need to be able to change the lowest number.

For some reason, instead of giving me a random number it gives a totally unrelated number, that isn't random either, but the same every time. My first thought was it was perhaps adding the second variable instead of making a randomizer. That is not the case though, and I'm not sure how to fix this issue.

I have also tried the following code:

set /a num1=10
set /a num2=(%random%*60/32768)+%num1%
echo %num2%

The issue with this code is it never seems to work as randomizer for me even without the variable.

Any help is appreciated.

8
  • 1
    I think you can apply the answer from here stackoverflow.com/a/5777608/2894590 Commented Oct 31, 2020 at 17:27
  • Batch is funny, so you might need the spaces and don't use the parens. Commented Oct 31, 2020 at 17:30
  • 2
    You should be using + num1, not +%num1%. for example set /a "num2=(%RANDOM% %% 60) + num1" Commented Oct 31, 2020 at 17:30
  • Also, which version of windows? Since earlier versions work differently. Commented Oct 31, 2020 at 17:32
  • @CookieButter I have the latest version of Windows 10... Perhaps I typed it in wrong. Also I believe that may work, I will try it. Commented Oct 31, 2020 at 18:08

2 Answers 2

1

Here's some examples to assist you:

Set "num1=10"
Set /A "num2 = (%RANDOM% %% 60) + num1"
Echo(%num2%
Set "num1=10"
Set /A "num2 = (%RANDOM% * 60 / 32768) + num1"
Echo(%num2%

Please note, that we have only been provided with a very small portion of your batch file, so if this code is part of a parenthesized block, you may need to enable delayed expansion and use !RANDOM! and possibly !num2! instead of %RANDOM% and %num2% respectively.

Sign up to request clarification or add additional context in comments.

Comments

0
  1. In the first form you are have to account for modulus bias, as described further in this question Why do people say there is modulo bias when using a random number generator?

(Note: the accepted answer there has a flaw which I pointed out and provide a solution to in my answer to that question.)

  1. In both the first and the second form they present an incorrect range of values the way it is written. More info can be found here How to use random in BATCH script?

Long story Short:

In both forms, if you want your range to start at 10 and go to 60 you will need to adjust it to be using a modulus of 50, in the first form you must account for modulus bias in your calculations causing some numbers to appear more often.

The first form can be fixed using this method:

SET "Min=10"
SET "Max=60"
SET /A "Discard= 32768 - ( ( ( 32768 %% (Min-Max) ) + 1 ) %% (Min-Max) )"
:Rand
  SET /A "num2=%random%"
  IF %num2% GTR %Discard% GOTO :Rand
SET /A "num2= num2 %% (Min-Max) + Min"
echo=%num2%

The second form can be fixed using this method:

SET "Min=10"
SET "Max=60"
SET /A "num2= %random% * ( Max - Min + 1 ) / 32768 + Min "
echo=%num2%

Example of Full script:

@(SETLOCAL
  ECHO OFF
)

CALL :Main

( ENDLOCAL
  EXIT /B
  pause
)

:Main
  SET "Min=10"
  SET "Max=60"
  SET /A "Discard= 32768 - ( ( ( 32768 %% (Min-Max) ) + 1 ) %% (Min-Max) )"
  ECHO=Form1:
  CALL :Form1
  ECHO=Form2:
  CALL :Form2
  pause

GOTO :EOF

:Form1
  SET "num2=%random%"
  IF %num2% GTR %Discard% GOTO :Form1
  SET /A "num2= num2 %% ( Min - Max ) + Min"
  echo=%num2%
GOTO :EOF

:Form2
  SET /A "num2= %random% * ( Max - Min + 1 ) / 32768 + Min "
  echo=%num2%
GOTO :EOF

Results:

C:\WINDOWS\system32>C:\Admin\SE\testrandom.cmd
Form1:
10
Form2:
31
Press any key to continue . . .

C:\WINDOWS\system32>

5 Comments

Neither of these worked. I copied and pasted both of them sepretley in their own batch files, and the first always resulted in -40, and the second always resulted in 30. Perhaps I missed something or there is some sort of additional code I need other than the basic @echo off and pause >nul. Also, is there imperfections in the code set /a num2=%random% %%60 +1. Because I have always used this as a randomizer and it has always worked but recently it has been not functioning. It'll give the proper number 80% of the time but the rest it will give number a bit higher than the max.
@VictorChavez I had used the wrong form, as %= doesn't work as expected for this usage. I changed to SET /A "num2= num2 %% ( Min - Max ) + Min" which makes the process explicit. I put in the actual cmd script I used to test this as well and resulting output.
@VictorChavez as for your statement that Also, is there imperfections in the code set /a num2=%random% %%60 +1. Because I have always used this Yes a couple. For one there is a Modulus Bias, for another the range will produce incorrect values using your original code.
@VictorChavez As for Part 1, The Modulus Bias, that is to say if you want 1 to 60 you will favor some numbers over others, the amount you favor those numbers depends on size of the range of values you choose to use, however the code presented in this answer accounts for it. You can see more detail about this here: stackoverflow.com/a/46991999/3985011
@VictorChavez As for Part 2 as you mentioned It'll give the proper number 80% of the time but the rest it will give number a bit higher than the max. Yes this is because you are choosing numbers from 0 to 60 and then adding 10, this is OK if you want your numbers to be between 10 and 70, but not if you want your numbers to be between 10 and 60. This si why the code I presented uses the min and Max variables to make sure the correct min and max are in place. More information on this is found here: stackoverflow.com/a/5777608/3985011

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.