Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 396
Mee Avatar asked Mar 12 '23 05:03

Mee


1 Answers

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.

like image 196
Andrew Li Avatar answered Mar 14 '23 19:03

Andrew Li