C language interval random number generation with srand () & rand () & time ()

Source: Internet
Author: User
Tags random seed

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]View plaincopy
  1. /*
  2. Define the int rand (void) function );
  3. Function Description rand () returns a random value ranging from 0 to rand_max.
  4. 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.
  5. For details about random seed, see srand ().
  6. The Return Value Returns a random value between 0 and rand_max. rand_max is defined in stdlib. h and its value is 2147483647.
  7. Example:
  8. */
  9. # Include <stdlib. h>
  10. # Include <stdio. h>
  11. Double doublerand (double A, double B );
  12. Int intrand (int A, int B );
  13. Int main (void)
  14. {
  15. Double I = doublerand (2.0, 9.0 );
  16. Int J = intrand (2, 9 );
  17. Printf ("% F \ n", I );
  18. Printf ("% d \ n", J );
  19. Return 0;
  20. }
  21. Double doublerand (double A, double B)
  22. {
  23. Double R;
  24. R = (double) rand ()/rand_max;
  25. Return A + R * (B-);
  26. }
  27. Int intrand (int A, int B)
  28. {
  29. Return (INT) doublerand (A, B );
  30. }
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]View plaincopy

 
  1. # Include <stdlib. h>
  2. # Include <stdio. h>
  3. # Include <time. h>
  4. Int main ()
  5. {
  6. For (INT I = 0; I <100000; I ++)
  7. {
  8. Srand (unsigned) Time (null ));
  9. Printf ("% d \ n", Rand ());
  10. }
  11. Return 0;
  12. }
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]View plaincopy
  1. /*
  2. # Include <stdlib. h>
  3. Void srand (unsigned seed );
  4. # Include <time. h>
  5. Time_t time (time_t * time );
  6. */
  7. # Include <stdlib. h>
  8. # Include <stdio. h>
  9. # Include <time. h>
  10. Int main ()
  11. {
  12. Int I;
  13. // Use the current system time as the seed
  14. Srand (unsigned) Time (null ));
  15. For (I = 0; I <10; I ++)
  16. {
  17. Printf ("% d \ n", Rand ());
  18. }
  19. Return 0;
  20. }
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]View plaincopy
  1. # Include <stdlib. h>
  2. # Include <stdio. h>
  3. # Include <time. h>
  4. # Define max_num 10000
  5. /* When insert a rand_num then check it */
  6. Int check (int A [], int I)
  7. {
  8. Int J;
  9. For (j = 0; j <I; j ++)
  10. If (* (a + J) = * (a + I ))
  11. Return 0;
  12. Return 1;
  13. }
  14. Int main ()
  15. {
  16. Int I;
  17. Int A [max_num];
  18. // Use the current system time as the seed
  19. Srand (unsigned) Time (null ));
  20. For (I = 0; I <10; I ++)
  21. {
  22. A [I] = rand ();
  23. If (check (A, I) = 0)
  24. {
  25. I --;
  26. Continue; // The number is the same with of one of the array number, so once again
  27. }
  28. Printf ("% d \ n", a [I]);
  29. }
  30. Return 0;
  31. }
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 ~~~ :) Source Address: http://www.software8.co/wzjs/cpp/1043.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.