求助:编写Batch脚本生成符合规则的6位随机数CSV文件
解决方案:生成符合规则的300组CSV随机数
需求概述
- 生成包含300组数据的CSV文件
- 每组为1-39范围内的6个不重复随机数
- 任意两行的重复数字不得超过2个
当前脚本问题
- 输出行内容持续变长,无法固定为6个数字
- 未实现行内去重及跨行重复数字校验功能
- 临时文件逻辑混乱,导致数据累积
当前脚本代码
@echo off setlocal enabledelayedexpansion for /L %%j in (1 1 10) do ( call:get_rand echo %%j ;!NUM[1]!;!NUM[2]!;!NUM[3]!;!NUM[4]!;!NUM[5]!;!NUM[6]! >> test.txt sort "get_six.txt" >> "get_sort.txt" for /f %%b in (get_sort.txt) do findstr "%%~b" "get_tmp.txt" >nul 2>&1 || echo %%b>>"get_tmp.txt" call:get_horiz rem call:del_get_files ) goto:EOF rem delete extra files: rem :del_get_files rem for %%z in (get_six.txt,get_sort.txt,get_tmp.txt) do ( rem if exist "%%z" del /q /f "%%z" rem ) rem goto:EOF rem get_tmp.txt vertical list to csv horizontal list.txt :get_horiz set var= for /f "tokens=*" %%c in (get_tmp.txt) do ( call set var=%%var%%,%%c ) SET var=%var:~1% echo !var! >> list.txt goto:EOF rem get first set of six random numbers :get_rand for /L %%i in (1 1 6) do ( call:get_n %%i rem get random number, 1 is the min, 39 is the max echo !num[%%i]! >> get_six.txt ) goto:EOF rem get random num[%%i] between 1 and 39 :get_n set /a num[%1] = %RANDOM% * 39 / 32768 + 1 goto:EOF
期望输出示例(list.txt)
03,11,17,24,28,37 11,13,25,26,36,39 02,09,15,23,28,37 03,13,14,26,28,32 08,11,21,22,34,36 02,13,16,26,27,31 01,06,14,20,27,31 07,09,20,23,33,34 ...
当前实际输出问题示例
10,28,39 10,28,39,14,20,22,36 10,28,39,14,20,22,36,25,34,35 10,28,39,14,20,22,36,25,34,35,18,31,32 10,28,39,14,20,22,36,25,34,35,18,31,32,19,29,7 10,28,39,14,20,22,36,25,34,35,18,31,32,19,29,7,11,17,21,23,33 10,28,39,14,20,22,36,25,34,35,18,31,32,19,29,7,11,17,21,23,33,12,37 10,28,39,14,20,22,36,25,34,35,18,31,32,19,29,7,11,17,21,23,33,12,37,24 10,28,39,14,20,22,36,25,34,35,18,31,32,19,29,7,11,17,21,23,33,12,37,24,15 10,28,39,14,20,22,36,25,34,35,18,31,32,19,29,7,11,17,21,23,33,12,37,24,15,27
修复后的完整脚本
@echo off setlocal enabledelayedexpansion :: 初始化输出文件 if exist "list.txt" del "list.txt" set "total_lines=300" :: 生成第一组符合要求的6个不重复数字 call :generate_valid_line echo !current_line! >> list.txt set "line_count=1" :: 生成剩余299组 :generate_next_line if !line_count! geq !total_lines! goto :cleanup call :generate_valid_line call :check_against_all_lines if !is_valid! equ 1 ( echo !current_line! >> list.txt set /a line_count+=1 ) goto :generate_next_line :: 生成一行6个1-39的不重复数字,格式为两位补零的CSV :generate_valid_line set "current_line=" set "used_nums=" set "count=0" :pick_num if !count! equ 6 goto :format_line set /a rand_num=%RANDOM% * 39 / 32768 + 1 :: 补零为两位 if !rand_num! lss 10 set "rand_num=0!rand_num!" :: 检查是否已使用 echo !used_nums! | findstr /c:"!rand_num!," >nul if not errorlevel 1 goto :pick_num set "used_nums=!used_nums!!rand_num!," set /a count+=1 goto :pick_num :format_line :: 移除末尾的逗号 set "current_line=!used_nums:~0,-1!" goto :eof :: 检查当前行与已生成所有行的重复数字是否不超过2个 :check_against_all_lines set "is_valid=1" for /f "tokens=*" %%l in (list.txt) do ( set "match_count=0" :: 将当前行拆分为单个数字 for %%n in (!current_line:,= !) do ( echo %%l | findstr /c:"%%n" >nul if not errorlevel 1 set /a match_count+=1 if !match_count! gtr 2 goto :invalid_line ) ) goto :eof :invalid_line set "is_valid=0" goto :eof :: 清理临时变量并退出 :cleanup endlocal echo 生成完成,共!total_lines!组数据。 goto :eof
关键逻辑说明
- 行内去重:生成随机数时通过
findstr检查是否已加入当前行,确保6个数字无重复 - 跨行重复校验:新生成的行与已存在的每一行逐一比对重复数字数量,超过2个则重新生成
- 格式规范:所有数字补零为两位,输出为标准CSV格式
- 循环控制:直到生成满300组符合要求的数据为止
内容的提问来源于stack exchange,提问作者Rene
相关产品推荐
相关产品推荐

