【问题标题】:Is it possible to make a randomly generated variable in batch? [closed]是否可以批量生成随机生成的变量? [关闭]
【发布时间】:2019-08-20 00:40:05
【问题描述】:

目前我正在批量制作一个随机生成世界的游戏。我目前正在研究生物群系生成,我正在尝试找出一种方法来生成每个变量,所以这些变量看起来像%biomenum[0,0]%%biomenum[0,1]%,并且可能会达到%biomenum[512,512]% 和低至%biomenum[-512,-512]

我目前的代码为 9 个不同的块生成了生物群系,但它本身并没有生成变量,并且为 -512、-512 和 512、512 之间的每个块输入此代码似乎并不是全部似是而非的

rem 0x 0y
set /A biomenum0x0y = %RANDOM% * 16 / 32768 + 1
rem 0x 1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum0x1y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum0x1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum0x1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum0x1y =  biomenum0x0y - 1
rem 1x 1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum1x1y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum1x1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum1x1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum1x1y =  biomenum0x0y - 1
rem 1x 0y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum1x0y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum1x0y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum1x0y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum1x0y =  biomenum0x0y - 1
rem 1x -1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum1xneg1y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum1xneg1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum1xneg1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum1xneg1y =  biomenum0x0y - 1
rem 0x -1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum0xneg1y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum0xneg1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum0xneg1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum0xneg1y =  biomenum0x0y - 1
rem -1x -1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenumneg1xneg1y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenumneg1xneg1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenumneg1xneg1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenumneg1xneg1y =  biomenum0x0y - 1
rem -1x 0y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenumneg1x0y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenumneg1x0y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenumneg1x0y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenumneg1x0y =  biomenum0x0y - 1
rem -1x 1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenumneg1x1y =  biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenumneg1x1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenumneg1x1y =  biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenumneg1x1y =  biomenum0x0y - 1
echo %biomenumneg1xneg1y% is the biome for -1,-1
echo %biomenumneg1x0y% is the biome for -1, 0
echo %biomenumneg1x1y% is the biome for -1, 1
echo %biomenum0xneg1y% is the biome for 0, -1
echo %biomenum0x0y% is the biome for 0, 0 -the starting chunk-
echo %biomenum0x1y% is the biome for 0, 1
echo %biomenum1xneg1y% is the biome for 1, -1
echo %biomenum1x0y% is the biome for 1, 0
echo %biomenum1x1y% is the biome for 1, 1

我如何能够自动创建变量,这样我就不必输入所有 1,048,576 个块?

*注意:这个问题已经过编辑,因此更有意义。原文是我在 3 个小时的睡眠中做出的很长的解释,对此我深表歉意。希望这更容易理解。

【问题讨论】:

标签: batch-file random


【解决方案1】:

你的解释很混乱,无论是你想得到什么,还是得到它的方式......

当你有一个一系列的变量同名并且你想选择一个变量基于另一个变量的值(称为index em>),那么这种数据结构的名称是array。如果数组elements只被一个索引变量(一维数组)选中,那么它通常被称为vector。如果数组元素被两个或多个索引选择(多维数组),则称为矩阵

在计算机程序中,在以相同方式处理的一系列值上重复相同代码的常用方法是通过 FOR 语句/构造/命令。 FOR 控制变量 放置在代码中以代替可变值。处理一系列数组元素的常用方法是通过 FOR 命令并将其控制变量作为数组元素的索引。

在批处理文件程序中处理数组元素的方法在this answer 中进行了描述。

下一个批处理文件获得与您的示例代码等效的结果,但使用二维数组和两个嵌套的 FOR 命令:

@echo off
setlocal EnableDelayedExpansion

rem 0x 0y
set /A biomenum0x0y = %RANDOM% * 16 / 32768 + 1
rem The rest!
for %%x in (neg1 0 1) do (
   for %%y in (neg1 0 1) do (
      set /A nextbiomenum = !RANDOM! * 4 / 32768 - 1, biomenum%%xx%%yy = biomenum0x0y + nextbiomenum %% 2
   )
)

echo %biomenumneg1xneg1y% is the biome for -1,-1
echo %biomenumneg1x0y% is the biome for -1, 0
echo %biomenumneg1x1y% is the biome for -1, 1
echo %biomenum0xneg1y% is the biome for 0, -1
echo %biomenum0x0y% is the biome for 0, 0 -the starting chunk-
echo %biomenum0x1y% is the biome for 0, 1
echo %biomenum1xneg1y% is the biome for 1, -1
echo %biomenum1x0y% is the biome for 1, 0
echo %biomenum1x1y% is the biome for 1, 1

此代码获得的结果与您的代码完全相同。在您的代码中,当随机数为 1 时加 1,当随机数为 4 时减 1(忽略 2 和 3)。在我的简化中,我生成一个介于 -1 和 2 之间的数字(而不是 1 到 4),并在 random 为 1 时加 1,在 random 为 -1 时减 1(忽略 0 和 2);这是用数字除以 2 的余数(mod,% 运算符)完成的。但是,统计结果是一样的......

安东尼奥

PS - 我建议您使用 标准数组表示法 以及用方括号括起来的下标(例如 %biomenum[1,0]%%biomenum[1][0]% 而不是 %biomenum1x0y% ),原因在 @ 中解释987654323@.

【讨论】:

  • 抱歉这个问题的解释,有时我在写的时候可能有点离题,但这正是我想要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-05
  • 1970-01-01
  • 2017-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多