-3

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.

Adam
  • 5,403
  • 6
  • 31
  • 38
ender_scythe
  • 470
  • 7
  • 16
  • possible duplicate of [Random generator in the batch](http://stackoverflow.com/questions/19694021/random-generator-in-the-batch) – SomethingDark Jul 06 '15 at 17:29

4 Answers4

1

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

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

UPDATE

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
....
Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
  • Please specify what the file does rather than just giving a link. – ender_scythe Jul 06 '15 at 17:19
  • @EnderScythe you might want to add platform tag. Wrt about this function it does fill the buffer with what you could consider truly random bits. Best option on windows platform for non-pseudo-random generator. I don't know how to call it from batch, but calling it from Powershell might be a better option – Severin Pappadeux Jul 06 '15 at 18:11
  • 1
    @SeverinPappadeux - the batch-file tag description clearly indicates that it's meant exclusively for DOS, OS/2, and Windows. Also, the link you've posted only mentions that the function works in C++, so if you have a way to run it in PowerShell, please add it to your answer. – SomethingDark Jul 06 '15 at 18:21
  • @SomethingDark That is exactly my point. While batch files used on several systems, entropy/cryptography random numbers generators are VERY platform specific. Thus, you have to specify your platform beyond just `batch` tag. See update wrt powershell – Severin Pappadeux Jul 06 '15 at 18:43
  • Can we just use BASIC batch, not .net or powershell? – ender_scythe Jul 06 '15 at 18:57
  • @EnderScythe probably you can (i don't know how), but at the end of the day it HAS to make a call to crypto/entropy source of random bits if you want truly random numbers – Severin Pappadeux Jul 06 '15 at 19:35
  • @EnderScythe: no, the Windows batch processor is far too primitive to contain built-in support for strong random number generation. – Harry Johnston Jul 06 '15 at 23:53
  • Ok, so i'm chasing an impossible desire... D': – ender_scythe Jul 07 '15 at 03:38
  • @EnderScythe looks like it. Apparently, %random% in batch is seeded with current time in seconds, so as you demonstrated if two batch files start within one second, they will have absolutely the same random numbers. The only solution I see is to have master batch which forks out your use-random batches and makes sure there is more than 1sec delay. Ugly! – Severin Pappadeux Jul 07 '15 at 03:43
1

On powershell it's pretty easy,

You can use the cmdlet Get-Random by itself or specify range like 1..100 | Get-Random

Another option is to call the System.Random object directly:

$Random = New-Object System.Random
$Random.Next()
Avshalom
  • 8,657
  • 1
  • 25
  • 43
1

The problem indeed is that %RANDOM% is only pseudo-random and depends when it's called; if two scripts use it (more-less) at the same time, it will have the same value.

Instead, call PowerShell from your batch script:

    @for /f "tokens=*" %%i in ('powershell -Command "(1..100 | Get-Random)"') do @(
        set "trueRandomInteger_between_1_and_100=%%i"
    )
numdig
  • 101
  • 4
0

Well, you may program a Batch file version of one of the tons of existent methods that generate pseudo-random numbers, or look for one that may be programmed in a Batch file with no further complications like The Minimal Standard Random Number Generator, or a simpler version of it like these Two Fast Implementations, and even modify one of they to made it simpler!

@echo off
setlocal EnableDelayedExpansion

rem Pseudo random number generator based on "The Minimal Standard"
rem http://www.firstpr.com.au/dsp/rand31/p87-carta.pdf
rem Antonio Perez Ayala aka Aacini

rem Initialize values
set /A a=16807, s=40

rem APA Modification: use current centiseconds for initial value
for /F "tokens=2 delims=." %%a in ("%time%") do if "%%a" neq "00" set /A s=1%%a %% 100


rem Example of use: generate and show 20 random numbers
for /L %%i in (1,1,20) do (
   call :RandomGen
   echo !rand!
)
goto :EOF



:RandomGen

rem Multiply the two factors
set /A s*=a

rem If the result overflow 31 bits
if %s% lss 0 (
   rem Get result MOD (2^31-1)
   set /A s+=0x80000000
)

rem APA modification: return just the low 15 bits of result (number between 0..32767)
set /A "rand=s & 0x7FFF"

exit /B

Of course, I know this method is not comparable with most of the "standard" methods, but I think it is enough for simple Batch file applications, like games...

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • It's technically pseudo-random... (it uses centiseconds) – ender_scythe Jul 07 '15 at 21:25
  • Well, this method is not "technically" pseudo-random; it is _really_ pseudo-random (as indicated in two lines in my answer). [This site](https://en.wikipedia.org/wiki/Random_number_generation) indicate: _A "random number generator" based solely on deterministic computation cannot be regarded as a "true" random number generator in the purest sense of the word..._ In other words: there is _no way_ to generate _true_ random numbers via a computer program, but this method solve in a very simple way the problem of generate the same numbers sequence in two or more simultaneous Batch files... – Aacini Jul 09 '15 at 02:57