Using some computer intelligent algorithms (GA, PSO, ANN etc .) during simulation, you often need to randomly generate the initial population (initial sample) to see <stdlib. h> Generate pseudo-random numbers for these two functions ~~~

1. Generate a real number and an integer between [a, B]

[Cpp]

/* Www.2cto.com

Define the int rand (void) function );

Function Description rand () returns a random value ranging from 0 to RAND_MAX.

Before calling this function to generate a random number, you must use srand () to set the random number seed. If no random number seed is set, rand () will automatically set the random number seed to 1.

For details about random seed, see srand ().

The Return Value Returns a random value between 0 and RAND_MAX. RAND_MAX is defined in stdlib. h and its value is 2147483647.

Example:

*/

# Include <stdlib. h>

# Include <stdio. h>

Double doubleRand (double a, double B );

Int intRand (int a, int B );

Int main (void)

{

Double I = doubleRand (2.0, 9.0 );

Int j = intRand (2, 9 );

Printf ("% f \ n", I );

Printf ("% d \ n", j );

Return 0;

}

Double doubleRand (double a, double B)

{

Double r;

R = (double) rand ()/RAND_MAX;

Return a + r * (B-);

}

Int intRand (int a, int B)

{

Return (int) doubleRand (a, B );

}

Each function in the above Code can generate only one random number. Why?

2. generate multiple random numbers

The reason why rand () has the same random number each time is that the rand () function is incorrectly used. Random numbers returned by various programming languages (pseudo-random numbers) are actually a set of values calculated based on the recursive formula. When the sequence is long enough, these values are approximately evenly distributed. If the initial values (called seeds) of the pseudo-random sequence are the same, the calculated pseudo-random sequence is exactly the same. This feature is used by software for encryption and decryption. During encryption, a pseudo-random sequence can be generated using the number of seeds for data processing. During decryption, a pseudo-random sequence is generated using the number of seeds and the encrypted data is restored. In this way, people who do not know the number of seeds need to spend more time to decrypt them. Of course, this completely identical sequence is very bad for you. To solve this problem, you must specify different seeds before each random sequence is generated, so that the calculated random sequence will not be identical. You can call srand (unsigned) time (NULL) before calling the rand () function, so that the time function value (that is, the current time) is used as the number of seeds, because the time for calling the rand function twice is usually different, this ensures randomness. You can also use the srand function to specify the number of seeds.

Well, I will write it like this ~~~

[Cpp]

# Include <stdlib. h>

# Include <stdio. h>

# Include <time. h>

Int main ()

{

For (int I = 0; I <100000; I ++)

{

Srand (unsigned) time (NULL ));

Printf ("% d \ n", rand ());

}

Return 0;

}

A: There is a problem with your program. Every time you generate a random number, you call srand once. Because the computer runs very fast, so every time you use time, you get the same time (time precision is low, only 55 ms ). This is equivalent to using the same seed to generate a random sequence, so the random numbers generated are always the same.

You should put srand out of the loop:

[Cpp]

/*

# Include <stdlib. h>

Void srand (unsigned seed );

# Include <time. h>

Time_t time (time_t * time );

*/

# Include <stdlib. h>

# Include <stdio. h>

# Include <time. h>

Int main ()

{

Int I;

// Use the current system time as the seed

Srand (unsigned) time (NULL ));

For (I = 0; I <10; I ++)

{

Printf ("% d \ n", rand ());

}

Return 0;

}

3. What if I want to repeat it? That is, the particles in the population are all different ~~~

First, the most stupid way is to take an array to store your random number, one by one, and check the side. This complexity increases with the number of groups ~~~ And the time is unpredictable, which is a bad message for RTOS ~~~

However, it is easy to implement ~~~

[Cpp]

# Include <stdlib. h>

# Include <stdio. h>

# Include <time. h>

# Define MAX_NUM 10000

/* When insert a Rand_Num then check it */

Int check (int a [], int I)

{

Int j;

For (j = 0; j <I; j ++)

If (* (a + j) = * (a + I ))

Return 0;

Return 1;

}

Int main ()

{

Int I;

Int a [MAX_NUM];

// Use the current system time as the seed

Srand (unsigned) time (NULL ));

For (I = 0; I <10; I ++)

{

A [I] = rand ();

If (check (a, I) = 0)

{

I --;

Continue; // the number is the same with of one of the array number, so once again

}

Printf ("% d \ n", a [I]);

}

Return 0;

}

This is similar to the shuffling algorithm, but it is not as strict as the card sharding rules. The application is different ~~~

In fact, the particles produced by a population can be repeated in many cases (related to the specific problem model )~~~

What about the stupid method? This is not as many rules as the shuffling algorithm ~~~

Well, at least the particle of the PSO can generate ~~~ :)