4

In the command prompt environment, there is a variable %random% that uses some algorithm to generate pseudo-random numbers.

Does anyone know the algorithm that generates these numbers?

Mee
  • 207
  • 5
  • 11
  • Your question is irrelevant. There is no programming contract therefore it is undefined. –  May 25 '16 at 04:29
  • And what you want to do with it? – piyushj May 25 '16 at 04:29
  • According to [this](http://ss64.com/nt/syntax-random.html) site, "In the case of the CMD `%RANDOM%` the seed is based on the clock time when the CMD session started. This can be problematic when running a batch file, if the script always takes about the same time to run before calling `%RANDOM%` then the number returned will always lie within a small predictable range.".. So that's interesting, anyway. – Blorgbeard May 25 '16 at 04:32
  • @Noodles I'm not quite sure I understand what you mean. Could you maybe elaborate? – Mee May 25 '16 at 04:33
  • 2
    https://blogs.msdn.microsoft.com/oldnewthing/20100617-00/?p=13673 – גלעד ברקן May 25 '16 at 04:33
  • @piyushjaiswal I _had_ some code that was behaving unexpectedly, and I managed to trace it back to `%random%`. – Mee May 25 '16 at 04:36
  • 3
    There is no documentation. Microsoft can and probably has changed how it works (x32 to x64). As it's not documented YOU CANNOT TAKE A DEPENDANCY ON OBSERVED BEHAVIOUR. PS Your question doesn't contain a problem. –  May 25 '16 at 04:36
  • 1
    @Noodles - That is a totally unpractical approach. There are countless undocumented, but critical facts about batch that can only be deduced by observed behavior. Without that knowledge, most of my scripts would be impossible to write. Just because a behavior is documented doesn't mean that the behavior has to remain in subsequent versions. Conversely, lack of documentation doesn't make it any more l likely that behavior will change in the future. Thankfully, Microsoft has a history of striving for backward compatibility.between versions of Windows when it comes to batch. – dbenham May 25 '16 at 11:49
  • 1
    Possible duplicate of [Random generator in the batch](http://stackoverflow.com/questions/19694021/random-generator-in-the-batch) – Peter O. May 25 '16 at 12:20

1 Answers1

7

The %random% dynamic variable generates a random number from 0 to 32,767 inclusive. The algorithm of which these numbers are generated from is this:

srand((unsigned)time(NULL));

It turns out that the Windows command processor uses the standard naïve algorithm for seeding the random number generator (Quote from here)

It spits out a new number every second because of the time seed.

As dbenham pointed out, two command prompts opened in the same second will output the same exact numbers because of pseudorandomness and the taking in of time as a seed.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
  • You should cite your source for that quote. – Blorgbeard May 25 '16 at 04:43
  • 2
    Officially, the algorithm from which these numbers are generated is UNDEFINED. You can't even take a dependency on this from version to version of Windows. – Ryan Bemrose May 25 '16 at 06:08
  • Nice reference! I had [deduced the behavior on my own using experimentation](http://stackoverflow.com/a/19697361/1012053). But it is nice to see confirmation from a more authoritative source. – dbenham May 25 '16 at 11:31
  • 1
    Actually, your last sentence is not quite right. Each expansion of `%RANDOM%` will always generate a new number, regardless of elapsed time. But the initial seed value for a given cmd.exe session is dependent on the time. So two command windows that are launched within the same second will generate the exact same sequence of numbers. – dbenham May 25 '16 at 11:56
  • I read the article at the given link and _NOT_ found a clear statement that confirms that "The algorithm that cmd.exe uses to generate random numbers is `srand()`". This is not confirmed data that, IMHO, should be removed from this answer because it may cause confusions. For example, @dbenham 's comment about the "more authoritative source" missed to clear that he refers to the time seed only, _not_ to the algorithm used. – Aacini May 25 '16 at 16:57
  • @AndrewL: The source indicate that "cmd.exe uses the standard naïve algorithm **FOR SEEDING** the random number generator" and the `srand()` code shown is just _an example_ of the seeding method. Even if a certain source would said that the algorithm for %random% numbers is XXX or YYY, I would _not_ trust it without a clear prove. I suggest you to remove your _"The cmd prompt uses the C method of srand"_ phrase from your answer. – Aacini May 25 '16 at 17:14
  • I see. Thanks for that! Done – Andrew Li May 25 '16 at 17:15
  • @Aacini - Given that srand() is used to seed the pseudo random number generator, then it is a safe bet that rand() is used to generate the random numbers. But as far as what algorithm is used to implement rand(), that depends on the compiler used by Windows. – dbenham May 25 '16 at 17:45
  • @dbenham: That site does _NOT_ specify that "cmd.exe uses `srand()` function to seed the random number generator", it said a _very different_ thing, but even in this case I would not trust on such statement without a definitive prove. This discussion is totally useless, because it is very easy to write a short C/C++ program that uses `srand()/rand()` C-Run-Time Win32 API functions to generate a series of random numbers and start it at the same time that a Batch file that do the same thing, so anyone with a C compiler installed could provide the final prove to accept or refuse this _hypothesis_ – Aacini May 26 '16 at 01:33
  • 1
    @Aacini - As I read it, the Raymond Chen blog very explicitly states that the Windows command processor uses srand() to seed the PRNG. I guess you and I interpret English differently. Raymond Chen's blog is not official MS documentation, but he was an important member of various Windows OS development teams, and is certainly in a position to know what he is talking about. – dbenham May 26 '16 at 03:40
  • The Raymond Chen blog is the personal blog of one person at Microsoft with a long memory and source code access. On his blog, he [disclaims](https://blogs.msdn.microsoft.com/oldnewthing/20070822-09/?p=25453) that he is NOT an official spokesman of Microsoft, and has said multiple times that his blog does NOT constitute official documentation. The most you can glean from that blog post is that this is how it works in the version of Windows Raymond was writing about (Windows 7 I'd guess by the date). You cannot take a dependency on this behavior and expect it not to change. – Ryan Bemrose Jun 01 '16 at 18:38