1

please give me the code for generating 4 digit random number in dos (batch) file

i tried

set /a num=%random% %%10000 +1

but this is not giving exactly 4 digit number every time. so please help me.

jarnosz
  • 243
  • 1
  • 18
Shivam Kumar
  • 21
  • 1
  • 2
  • 1
    Possible duplicate of [How to use random in BATCH script?](http://stackoverflow.com/questions/5777400/how-to-use-random-in-batch-script), for your upper limit, simply use `9999`. – Matt Clark Apr 08 '16 at 15:26
  • `set /A NUM=%Random% %%10000` generates random numbers in the range from `0` to `9999`; to have always 4 digits, you need to pad leading zeros: `set NUM=000%NUM% & set NUM=%NUM:~,-4%`; – aschipfl Apr 08 '16 at 15:39
  • @aschipfl there is a comma too much: `set NUM=%NUM:~-4%` – Stephan Apr 08 '16 at 15:54
  • @Stephan, you are right, thanks for the hint... – aschipfl Apr 08 '16 at 16:07
  • `0001` is not a 4-digit number. It's still a 1-digit number but with 3 padding 0s – phuclv Apr 08 '16 at 16:26
  • ... and `2016` is a two-digit-number with padding `20`? If this were MathOverflow, you were right, but programmers think differently. – Stephan Apr 08 '16 at 16:46
  • The OP did not state clearly what he needs; either numbers from `0000` to `9999`, or from `1000` to `9999`; I assumed the first range; if he needs the latter one, the solution is quite obvious, I think: `set /A NUM=%Random% %%9000 +1000`... – aschipfl Apr 08 '16 at 18:48

5 Answers5

4

just to add another possibility:

echo %random:~-1%%random:~-1%%random:~-1%%random:~-1%

(take the last digit from four randoms)

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • this would provide more uniformity in distribution compared to other solutions – phuclv Apr 08 '16 at 16:29
  • This pushes the uniformity error down to 0.03% (1 / 3276) per digit, but it's still there. – Ryan Bemrose Apr 08 '16 at 19:20
  • @RyanBemrose no programmer expects random numbers (generated by an algorithm) to be truly random. – Stephan Apr 08 '16 at 19:40
  • Yes, but there's several orders of magnitude difference between 1/3276 and the 1/several-billion error introduced by using pseudorandom generator. The latter no programmer is ever likely to observe. The former can be seen by taking 10 minutes to run the calculation a few thousand times. – Ryan Bemrose Apr 08 '16 at 19:46
  • I'm not buying the claims of a more uniform distribution. Assuming %RANDOM% has decent randomness, then there is a very slight bias towards digits 0,1,2,3,4,5 appearing more frequently than digits 6,7,8,9. But this is a clever way to get a 0 padded 4 digit number in one step. – dbenham Apr 09 '16 at 04:22
1

You don't say whether you need uniformity over the entire range, or if you just want a quick 4-digit number for a filename.

One super quick way is to limit your range to 1000-9999

set /a num = %random% %% 9000 + 1000

If you want the full range from 0000-9999, you can take a larger number and truncate to get the last four digits, but only after guaranteeing that you have at least four digits. To do this, add 1000 to the number before truncating.

set /a num = %random% + 1000
set num=%num:~-4%

This still suffers from a uniformity problem. Numbers between 0000-2767 will appear about 33% more often than numbers between 2768-9999, because of the way %random% generates numbers.

One way to reduce this error (but not eliminate it) is to take the result of several %random% calls together.

set num=%random:~-1%%random:~-1%%random:~-1%%random:~-1%

In this case, digits 0-7 are still about 0.1% more likely than 8-9, which is good enough for most anything.

BUT, if you want to remove the error entirely, you can do so with a loop.

:rndloop
set /a num = %random% + 1000
if %num% GEQ 31000 goto :rndloop
set num=%num:~-4%

This loop will always give you a uniform distribution between 0000 and 9999.

Credit to @aschipfl, @Manuel, @Stephan, and @Magoo for the parts of their answers borrowed here.

Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54
0

Try this:

set random_number=%random:~-4%

This will generate a 4digit random number

Manuel
  • 46
  • 1
  • 5
0

Crudely,

:rndloop
set /a random_number=%random%+10000
if %random_number% geq 40000 goto rndloop
set "random_number=%random_number:~-4%"
echo %random_number%

If you actually want a random number in range 0..9999 then try

:rndloop
set /a random_number=%random%
if %random_number% geq 30000 goto rndloop
set /a random_number=random_number/3
echo %random_number%

Assuming %random% is linear between 0 and 32767, then numbers greater than 29999 must be dismissed as using them would bias the distribution:

800 would be generated for %random%=800 or 10800 or 20800 (3 possible)
200 would be generated for %random%=200 or 10200 or 20200 or 30200 (4 possible)

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

Try this:

set num=0000%random%
set num=%num:~-4%

This will generate a random number between 0000 and 9999