Shuffling (Arrangement)

Source: Internet
Author: User

Display array content in GDB: P (INT [10]) *

I:

1 # include <stdio. h>
2 # include <stdlib. h>
3 # include <time. h>
4
5 # define num 10
6 const int shuffle_initial [num] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
7 int shuffle_flags [num] = {0 };
8 int shuffle_final [num] = {0 };
9
10 int generate (void)
11 {
12 int I, K, J, L, temp;
13 srand (unsigned INT) Time (null ));
14 For (I = num; I --)
15 {
16 k = 0;
17 J = rand () % I + 1;
18 do
19 {
20 if (shuffle_flags [k ++] = 0)
21 -- J;
22} while (j );
23
24 -- K;
25 shuffle_final [k] = shuffle_initial [k];
26 shuffle_flags [k] = 1;
27 printf ("% d \ t", shuffle_final [k]);
28}
29 printf ("\ n ");
30 return 0;
31}

 

II,

1 # include <stdio. h>
2 # include <stdlib. h>
3 # include <time. h>
4
5 # define num 10
6 int shuffle [num] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
7
8 int generate (void)
9 {
10 int I, K, temp;
11 srand (unsigned) Time (null ));
12 For (I = num; I! = 1 ;)
13 {

/* It should be noted that the random function is not the ansic standard, so the random function should not be used in programs compiled by GCC,

If you only use rand (), a random number is generated. The problem is that the random number generated each time is the same !!!

In the GCC environment, you need to use srand () to plant a seed. The seed should be different every time,

The random number sequence is the same every time when fixed seeds are planted.

*/
14 temp = rand () % I;
15 I --;
16 k = shuffle [temp];
17 shuffle [temp] = shuffle [I];
18 shuffle [I] = K;
19
20 printf ("% d \ t", k );
21}
22 printf ("% d \ n", shuffle [0]);
23
24 return 0;
25}

 

Related Article

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.