批处理中的随机生成器 [英] Random generator in the batch

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

问题描述

我有一个bat文件

@echo %RANDOM%

并使用命令行执行它

start randomcheck.bat & start randomcheck.bat

打开了两个控制台,并且都包含相同的数字 4645.这不符合随机 提供不同的临时文件夹(我只在同时使用它们时才需要随机文件夹).你如何批量生产普通的随机生成器?

Two consoles are opened and both contain the same number, 4645. This fails the purpose of random to provide different temp folders (I need random folders only when use them simulataneously). How you have normal random generator in batch?

更新https://stackoverflow.com/a/19697361/1083704 已经量化了全球种子更新周期.windows shell 全局种子每秒更新一次.实际上,我必须再增加一秒钟的安全裕度,以防止比赛并希望这是足够的措施.这真的很糟糕.这意味着为我的 iCore7 启动 8 个进程需要 16 秒.而且我仍然不确定这是否会成功,因为没有正式指定,尽管最初有 8 个进程以相对时间偏移启动,但可能会发生两个进程同时完成,我必须注意它们是没有再次同时重新启动.这完全是胡说八道,我的问题是可以在批处理中解决这个问题,而无需求助于 C++ 或 VBScript 吗?

Update https://stackoverflow.com/a/19697361/1083704 has quantified the global seed update period. The windows shell global seed is updated every second. Practically, I must add one more second for safety margin, to prevent the races and hope that this is sufficient measure. This really sucks. It means that launching 8 processes for my iCore7 will take 16 seconds. And I am still not sure that this succeeds since nothing is officially specified and it is still possible that despite 8 processes are started initially with relative time shift, it may happen that two processes finish at the same time and I must take care that they are not restarted simultaneously again. This is complete bullshit and my question was can this be cured in the batch, without resorting to C++ or VBScript?

推荐答案

$RANDOM in bash 没有这个缺陷.

$RANDOM in bash does not have this flaw.

@echo win=%RANDOM%

@for /f %%i in ('bash -c "echo $RANDOM"') do @set VAR=%%i

@echo cygwin=%VAR%

当我运行 start randomcheck.bat & 时打印相同的 Windows 编号但不同的 cygwin 编号启动 randomcheck.bat.使用 cygwin 比为单个命令编写单独的 VBScript 更好.也许你可以教我如何用 Windows 脚本主机替代品替换 bash -c "echo $RANDOM" ,这将消除安装 cygwin 的需要.

prints the same windows numbers but distinct cygwin ones when I run start randomcheck.bat & start randomcheck.bat. Using cygwin is better than writing a separate VBScript for a single command. Might be you can teach me how to replace the bash -c "echo $RANDOM" with the windows scripting host alternative, which would eliminate the need for installing the cygwin.

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

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