在Windows批处理文件中设置随机数组 [英] Setting up a random array in a Windows batch file

查看:46
本文介绍了在Windows批处理文件中设置随机数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个批处理文件,该文件通过Steam Achievement Manager一次启动10个Steam游戏.这是我正在使用的当前脚本(其中包括10多个脚本):

I'm trying to set up a batch file that launches 10 Steam games, through Steam Achievement Manager, at a time. Here is the current script I am using (which includes many more than 10):

@echo off
echo Launching Steam games...
start sam.game.exe 233720
start sam.game.exe 113200
start sam.game.exe 219640
start sam.game.exe 2500
start sam.game.exe 204300
start sam.game.exe 49600
start sam.game.exe 107100
start sam.game.exe 730
start sam.game.exe 550
start sam.game.exe 35700
start sam.game.exe 92300
start sam.game.exe 217690
start sam.game.exe 620
start sam.game.exe 8930
start sam.game.exe 57690
start sam.game.exe 24240
start sam.game.exe 3830
start sam.game.exe 35140
start sam.game.exe 200260
start sam.game.exe 4920
start sam.game.exe 245070
start sam.game.exe 204360
start sam.game.exe 46750
start sam.game.exe 300
start sam.game.exe 18500
start sam.game.exe 63800
start sam.game.exe 214340
start sam.game.exe 25800
start sam.game.exe 8500
start sam.game.exe 212680
start sam.game.exe 4000
start sam.game.exe 41800
start sam.game.exe 220
start sam.game.exe 25890
start sam.game.exe 219150
start sam.game.exe 1250
start sam.game.exe 42170
start sam.game.exe 202090
start sam.game.exe 220860
start sam.game.exe 4700
start sam.game.exe 32200
start sam.game.exe 22100
start sam.game.exe 39800
start sam.game.exe 91900
start sam.game.exe 40300
start sam.game.exe 4760
start sam.game.exe 45100
start sam.game.exe 11200
start sam.game.exe 107200
start sam.game.exe 99900
start sam.game.exe 440
start sam.game.exe 105600
start sam.game.exe 207610
start sam.game.exe 206440
start sam.game.exe 24010
start sam.game.exe 46540
start sam.game.exe 13230
start sam.game.exe 42960
pause

您看到的数字是Steam游戏的ID.基本上,我希望能够创建一个数组,其中包含我拥有的所有Steam游戏ID,以及一个脚本以随机选择10个(非重复的)且仅启动10个的脚本.批处理文件,所以我对此很麻烦.

The numbers that you see there are Steam game IDs. Basically, I want to be able to create an array filled with all of the Steam game IDs that I own, and the script to pick 10 of them (non-duplicating) at random and only start these 10. I'm very new with batch files so I am having a lot of trouble with this.

推荐答案

@echo off
setlocal EnableDelayedExpansion

rem You may define next value from a parameter, if you wish
set number=10

rem Define the array of game ID's
set n=0
for %%a in (233720 113200 219640 2500 204300 49600 107100 730   550    35700
            92300  217690 620    8930 57690  24240 3830   35140 200260 4920
            245070 204360 46750  300  18500  63800 214340 25800 8500   212680
            4000   41800  220
           ) do (
   set /A n+=1
   set gameID[!n!]=%%a
)

rem Select %number% non-duplicated random elements from previous array and run such Steam games
for /L %%n in (1,1,%number%) do (
   rem Get the index of a random element
   call :getRandomElem i=
   rem Run such game
   for /F %%i in ("!i!") do start sam.game.exe !gameID[%%i]!
   rem Put a zero in such element
   set gameID[!i!]=0
)

goto :EOF


rem Get the index of a random element greater than 0 from gameID array
:getRandomElem i=
set /A i = n * %random% / 32768 + 1
if !gameID[%i%]! equ 0 goto getRandomElem
exit /B

您可以在 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆