Mee
Mee

Reputation: 207

How are command prompt random numbers generated?

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?

Upvotes: 4

Views: 1792

Answers (1)

Andrew Li
Andrew Li

Reputation: 57964

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.

Upvotes: 7

Related Questions