如何批量获得真正的随机数? [英] How do I get truly random numbers in batch?

查看:119
本文介绍了如何批量获得真正的随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人都在某一点或另一点使用随机数.因此,我需要从命令提示符处生成一些真正的随机数(而不是伪随机数*),我还需要一个在代码行中生成大小为set RANDOM=%random% %%2 +1的字母的系统.而且我只是想捉住一只不存在的蝴蝶吗? * pseudo-random是依赖于时间等外部信息的随机数据,batch的随机数生成器是基于时间的伪随机数据,以测试此打开的2个新记事本.bat并将此文件命名为1.bat
1.bat

Everyone uses random numbers at one point or another. So, I need some truly random numbers (not pseudo-random*) generated from the command prompt, I also want a system that generates letters in a code line the size of: set RANDOM=%random% %%2 +1. And am I just trying to catch a non-existent butterfly? *pseudo-random is random that relies on outside info like time, batch's random generator is pseudo-random based on time, to test this open 2 new notepad .bat and name this file 1.bat
1.bat

start 2.bat
@echo off
cls
echo %random%
pause

2.bat

@echo off
cls
echo %random%
pause

发生了什么?!?好吧,这是伪随机的,只要打开批处理文件之间没有延迟,则批处理文件的编号将相同.

What's happening?!? Well this is pseudo-random, as long as there is NO delay between the opening of the batch files, the batch file's numbers will be the same.

推荐答案

您基本上是在请求熵池,而您的平台是Windows,对吗?

You're basically asking for entropy pool, and your platform is Windows, right?

最好的选择是使用CryptGenRandom()功能,请参见 https://msdn.microsoft.com/zh-CN/library/windows/desktop/aa379942(v = vs.85).aspx .您可能可以从Powershell调用它

Best bet is to use CryptGenRandom() function, see https://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx. You could probably call it from Powershell

更新

显然,有一个用于加密的.NET包装器,因此您可以从Powershell中使用它.

Apparently, there is a .NET wrapper for crypto, so you could use it from Powershell

[System.Security.Cryptography.RNGCryptoServiceProvider] $rng = New-Object System.Security.Cryptography.RNGCryptoServiceProvider

$rndnum = New-Object byte[] 4
# Generate random sequence of bytes 
$rng.GetBytes($rndnum)

# rndnum is filled with random bits
....

这篇关于如何批量获得真正的随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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