随机数生成器在批处理文件中不是随机的 [英] Random number generator not random in a batch file

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

问题描述

尝试生成随机数并将其用于Windows批处理脚本文件中的变量值.也许我缺少一些非常基本的东西,但是某些东西对我来说不起作用.

Trying to generate a random number and use it for variable value in Windows batch script file. Maybe I am missing something very basic, but something is not working here for me.

使用以下内容创建了名为random_test.bat的批处理文件.

Created a batch file named random_test.bat with the following content.

SET rnumber=%random%
echo %random%
pause

连续运行文件三次会产生以下输出:

Running the file consecutively three times produces the following set of outputs:

一个

C:\Users\user\Desktop>SET rnumber=28955

C:\Users\user\Desktop>echo 20160
20160

C:\Users\user\Desktop>pause
Press any key to continue . . .

两个

C:\Users\user\Desktop>SET rnumber=29072

C:\Users\user\Desktop>echo 13887
13887

C:\Users\user\Desktop>pause
Press any key to continue . . .

三个

C:\Users\user\Desktop>SET rnumber=29183

C:\Users\user\Desktop>echo 18885
18885

C:\Users\user\Desktop>pause
Press any key to continue . . .

如您所见,命令echo %random%会一直产生预期的0到32,767之间的相对随机数.

As you can see the command echo %random% keeps producing relatively random numbers between 0 and 32,767 as expected.

同时不使用%random%设置变量rnumber的值.它会产生一个不太随机的数字(可能也在0到32,767之间),但是它不是随机的.如果我现在要猜测的话,它似乎正朝着0到32,767的方向缓慢增长.

At the same time using %random% to set a value for variable rnumber does not. It produces a not-so random number (possibly too between 0 and 32,767) but it is not random. If I were to guess right now it seems to be slowly growing in the 0 to 32,767 direction.

为澄清起见,脚本在每次执行时都会在第2行产生一个随机数(20160、13887、18885 ...),但是第1行似乎会产生一个随着每次执行批处理文件而不断增加的数字(28955, 29072、29183等在我的多个测试中).

To clarify the script keeps producing a random number with line 2 on each execution (20160, 13887, 18885...), but line 1 seems to produce a number that keeps increasing with each execution of the batch file (28955, 29072, 29183, and so on in my multiple tests).

我已经在两台不同的计算机(分别为Windows 7 x64和Windows 2012 R2)上进行了尝试,并连续多次运行了这3行脚本.

I already tried it on two different computers, Windows 7 x64 and Windows 2012 R2 respectively, running this 3 line script multiple times in row.

接下来要尝试的事情将是来自完全不同的网络上的计算机,我想知道这是否与域策略,网络,软件有关.

Next thing to try will be on a computer from a completely different network, I'm wondering if this has anything to do with domain policies, network, software..

这是怎么回事?

从同一CMD窗口中依次运行命令时,它可以按预期工作,但多次执行同一批处理文件时,则无法正常工作.

When running the commands in sequence from the same CMD window it works as expected, but it does not when executing the same batch file multiple times.

  • 脚本echo %random%中的行按预期方式工作.
  • SET rnumber=%random%行没有.
  • The line in the script echo %random% works as expected.
  • The line SET rnumber=%random% does not.

(多次执行同一脚本时)

(When executing the same script multiple times)

推荐答案

提供从评论讨论,外部资源和关于我的原始问题的测试中学到的所有内容.

To provide a summary of everything that was learned from the comment discussion, external resources and testing regarding my original question.

打开新的CMD窗口时,批处理脚本中的随机数生成器使用以下算法为初始随机数值播种. (从这里开始- https://devblogs.microsoft.com/oldnewthing/?p=13673 )

Random number generator in batch script uses the following algorithm to seed the initial random number value when a new CMD window is opened. (from here - https://devblogs.microsoft.com/oldnewthing/?p=13673)

srand((unsigned)time(NULL));

因此,当在同一秒内启动两个(或多个)CMD窗口时,%random%的初始返回值是相同的.在具有相同种子和运行时间(一秒)的两个CMD窗口中,随后的%random%返回也相同.

As a result when two(or more) CMD windows are started within the same second the initial returns of the %random% are the same. Subsequent returns of %random% within two CMD windows with the same seed and run time (to a second) are also the same.

此外,如果因此以彼此之间有一定延迟的方式启动CMD窗口,则每个新CMD窗口中%random%伪变量的首次返回将远离随机性,并且将缓慢增加(0到32767范围).在我的测试中,每个新窗口的初始种子数量每秒增加3或4.

Moreover if CMD windows are consequently started with certain delays from each other the first return of the %random% pseudo-variable in each new CMD window will be far from random and will be slowly increasing (0 to 32767 range). In my testing this initial seed number grows by either 3 or 4 every one second for each new window.

要了解更多信息并找到解决方法,请点击此处:

To learn more about this and to find workarounds look here:

为什么rand()每次运行都会产生相同的数字序列吗?

还要查看@lit提供的PowerShell解决方法.

Also look at PowerShell workaround provided by @lit.

这篇关于随机数生成器在批处理文件中不是随机的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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