Question #5893   Submitted by Answiki on 02/22/2022 at 10:44:53 AM UTC

In C, what is the purpose of the srand() function?

Answer   Submitted by Answiki on 09/16/2022 at 08:18:36 PM UTC

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 :

7 events in history
Answer by Answiki on 09/16/2022 at 08:18:36 PM

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 :

Answer by Answiki on 02/22/2022 at 07:24:53 PM

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 :

Answer by Answiki on 02/22/2022 at 07:17:34 PM

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 :




Answer by Answiki on 02/22/2022 at 07:17:29 PM

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 :




Answer by Answiki on 02/22/2022 at 07:17:12 PM

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 :




Answer by Answiki on 02/22/2022 at 11:11:35 AM

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 :




Question by Answiki 02/22/2022 at 10:44:53 AM
In C, what is the purpose of the srand() function?

Icons proudly provided by Friconix.