%random%变量未返回随机数 [英] %random% variable not returning a random number

查看:194
本文介绍了%random%变量未返回随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码:

set /a alg=!random!*!activecount!/32768+1

在大多数情况下,它可以工作.但是,!random!变量不会生成完全随机的数字.相反,它以缓慢的速度不断增加.现在,假设变量!activecount!等于2,它会很长时间不间断地不断生成1或2. 为什么随机变量不随机化?我试过在事实之后回显!random!,然后它会生成一个随机数.有什么作用?

For the most part, it works. HOWEVER, the !random! variable isn't generating a completely random number. instead, it's constantly counting up at a slow pace. Now, assuming the variable !activecount! is equal to 2, it would constantly generate either 1 or 2, without changing, for a VERY long time. Why isn't the random variable randomizing? I tried echoing !random! after the fact, and it generates a random number then. What gives?

推荐答案

您的代码显示不够,但是我想我知道您的问题是什么.

You haven't shown enough of your code, but I think I know what your problem is.

您的代码可以在一个命令会话中的简单循环中正常工作:

Your code works fine in a simple loop within one command session:

@echo off
setlocal enableDelayedExpansion
set "activecount=10"
for /l %%n in (1 1 10) do (
  set /a alg=!random!*activecount/32768+1
  echo !alg!
)

-输出-

7
8
6
6
9
4
3
1
8
9

但是,如果每个!random!都行不通!在新的cmd.exe会话中.这是因为随机序列是在cmd.exe启动时启动的,并且种子值是从当前时间得出的,并且种子每秒仅更改一次.参见 https://stackoverflow.com/a/19697361/1012053

But it does not work if each !random! is in a new cmd.exe session. That is because the random sequence is initiated upon cmd.exe startup, and the seed value is derived from the current time, and the seed only changes once per second. See https://stackoverflow.com/a/19697361/1012053

这是您在新cmd.exe会话中每次执行的代码:

Here is your code with each execution in a new cmd.exe session:

@echo off
setlocal disableDelayedExpansion
set "activecount=10"
for /l %%n in (1 1 10) do (
  cmd /v:on /c "set /a !random!*activecount/32768+1"
  echo(
)

-输出-

6
6
6
6
6
6
6
6
6
6

如果我们打印出原始的!random,就更容易看到发生了什么!值,如果我们在5个群集之间放置1秒的延迟:

It is easier to see what is happening if we print out the raw !random! value, and if we put a 1 second delay between each cluster of 5:

@echo off
for /l %%n in (1 1 5) do (
  for /l %%m in (1 1 5) do (
    cmd /v:on /c "set /a !random!"
    echo(
  )
  timeout /t 1 >nul
  echo(
)

-输出-

21755
21755
21755
21755
21755

21758
21758
21758
21758
21758

21761
21761
21761
21761
21761

21764
21764
21764
21764
21764

21768
21768
21768
21768
21768

在任何给定的秒内,种子值保持恒定,因此第一个随机数恒定.但是,每隔一秒钟,种子值就会增加一小部分.给定用于将随机"数限制在较小范围内的公式,您可以看到更改结果值将花费很长时间.

Within any given second, the seed value remains constant, so the first random number is constant. But upon each new second, the seed value is incremented a small amount. Given the formula you use to restrict the "random" number to a small range, you can see that it will take a long time for the resultant value to change.

请记住,第一个代码演示了伪随机数在同一会话中有效.但是,在同一秒内开始的每个cmd.exe会话都会获得相同的种子值,因此每个会话都将获得完全相同的伪随机序列.

Remember that the first code demonstrates that the pseudo random number works within the same session. But every cmd.exe session that starts within the same second gets the same seed value, so each of those sessions gets the exact same pseudo random sequence.

这篇关于%random%变量未返回随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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