In C, what is the purpose of the srand() function?
The srand() function is used to initialize the pseudo-random generator, because the rand() function does not really draw numbers at random.
Computers are unable to produce real random numbers, they classically use pseudo-random generators that actually generate sequences of numbers that have properties similar to random numbers.
The problem is that, with identical initial conditions, the generated sequences are always identical. To obtain different sequences of numbers, which is generally the case when using a pseudo-random generator, it is necessary to initialize the sequence in a different way. This is called the seed. The function srand (unsigned int seed) allows to initialize the pseudo-random generator.
The solution commonly adopted to initialize the generator is to use the current time of the machine which, as it is constantly changing, will produce seeds and therefore numbers which will be different at each moment. Hence the execution of the following code:
// initialize the pseudo-random generator
rand (time(NULL))
See also :
The srand() function is used to initialize the pseudo-random generator, because the rand() function does not really draw numbers at random.
Computers are unable to produce real random numbers, they classically use pseudo-random generators that actually generate sequences of numbers that have properties similar to random numbers.
The problem is that, with identical initial conditions, the generated sequences are always identical. To obtain different sequences of numbers, which is generally the case when using a pseudo-random generator, it is necessary to initialize the sequence in a different way. This is called the seed. The function srand (unsigned int seed) allows to initialize the pseudo-random generator.
The solution commonly adopted to initialize the generator is to use the current time of the machine which, as it is constantly changing, will produce seeds and therefore numbers which will be different at each moment. Hence the execution of the following code:
// initialize the pseudo-random generator
rand (time(NULL))
See also :
The srand() function is used to initialize the pseudo-random generator, because the rand() function does not really draw numbers at random.
Computers are unable to produce real random numbers, they classically use pseudo-random generators that actually generate sequences of numbers that have properties similar to random numbers.
The problem is that, with identical initial conditions, the generated sequences are always identical. To obtain different sequences of numbers, which is generally the case when using a pseudo-random generator, it is necessary to initialize the sequence in a different way. This is called the seed. The function srand (unsigned int seed) allows to initialize the pseudo-random generator.
The solution commonly adopted to initialize the generator is to use the current time of the machine which, as it is constantly changing, will produce seeds and therefore numbers which will be different at each moment. Hence the execution of the following code:
// initialize the pseudo-random generator
rand (time(NULL))
See also :
The srand() function is used to initialize the pseudo-random generator, because the rand() function does not really draw numbers at random.
Computers are unable to produce real random numbers, they classically use pseudo-random generators that actually generate sequences of numbers that have properties similar to random numbers.
The problem is that, with identical initial conditions, the generated sequences are always identical. To obtain different sequences of numbers, which is generally the case when using a pseudo-random generator, it is necessary to initialize the sequence in a different way. This is called the seed. The function srand (unsigned int seed) allows to initialize the pseudo-random generator.
The solution commonly adopted to initialize the generator is to use the current time of the machine which, as it is constantly changing, will produce seeds and therefore numbers which will be different at each moment. Hence the execution of the following code:
// initialize the pseudo-random generator
rand (time(NULL))
See also :
The srand() function is used to initialize the pseudo-random generator, because the rand() function does not really draw numbers at random.
Computers are unable to produce real random numbers, they classically use pseudo-random generators that actually generate sequences of numbers that have properties similar to random numbers.
The problem is that, with identical initial conditions, the generated sequences are always identical. To obtain different sequences of numbers, which is generally the case when using a pseudo-random generator, it is necessary to initialize the sequence in a different way. This is called the seed. The function srand (unsigned int seed) allows to initialize the pseudo-random generator.
The solution commonly adopted to initialize the generator is to use the current time of the machine which, as it is constantly changing, will produce seeds and therefore numbers which will be different at each moment. Hence the execution of the following code:
// initialize the pseudo-random generator
rand (time(NULL))
See also :
The srand() function is used to initialize the pseudo-random generator, because the rand() function does not really draw numbers at random.
Computers are unable to produce real random numbers, they classically use pseudo-random generators that actually generate sequences of numbers that have properties similar to random numbers.
The problem is that, with identical initial conditions, the generated sequences are always identical. To obtain different sequences of numbers, which is generally the case when using a pseudo-random generator, it is necessary to initialize the sequence in a different way. This is called the seed. The function srand (unsigned int seed) allows to initialize the pseudo-random generator.
The solution commonly adopted to initialize the generator is to use the current time of the machine which, as it is constantly changing, will produce seeds and therefore numbers which will be different at each moment. Hence the execution of the following code:
// initialize the pseudo-random generator
rand (time(NULL))
See also :
The srand() function is used to initialize the pseudo-random generator, because the rand() function does not really draw numbers at random.
Computers are unable to produce real random numbers, they classically use pseudo-random generators that actually generate sequences of numbers that have properties similar to random numbers.
The problem is that, with identical initial conditions, the generated sequences are always identical. To obtain different sequences of numbers, which is generally the case when using a pseudo-random generator, it is necessary to initialize the sequence in a different way. This is called the seed. The function srand (unsigned int seed) allows to initialize the pseudo-random generator.
The solution commonly adopted to initialize the generator is to use the current time of the machine which, as it is constantly changing, will produce seeds and therefore numbers which will be different at each moment. Hence the execution of the following code:
// initialize the pseudo-random generator
rand (time(NULL))
Voire aussi :
- Comment tirer un nombre aléatoire en C ?
- Dans quelle bibliothèque se trouve la fonction rand() en C ?
| # | ID | Query | URL | Count |
|---|---|---|---|---|
| 0 | 12964 | en | https://en.ans.wiki/5893/in-c-what-is-the-purpose-of-the-srand-function | 7 |