0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

1つのbatファイルで乱数生成する方法+0埋めver

Last updated at Posted at 2020-01-25

概要

Windows環境で簡易テストを実施時、乱数を引数に入れたい時があります。
startcallで別ファイルを呼ばなくてもよい方法です。

乱数生成方法

random.bat
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /l %%i in (1,1,10) do (
for /f "usebackq" %%i in (`echo !random!`) do echo %%i
)

結果

> random.bat
4718
9387
21150
28469
26828
1503
26958
18045
12847
22602

頭0埋めしたい場合

以下のように書き換えます。

random.bat
@echo off
setlocal ENABLEDELAYEDEXPANSION
set num=0000
set result=
for /l %%i in (1,1,10) do (
set result=!num!!random!
for /f "usebackq" %%i in (`echo !result:~-5^,5!`) do echo %%i
)

結果

> random.bat
02040
06523
23965
21666
23812
05243
29043
12515
03643
01833
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?