The C language saves the random numbers generated into the array, and the data cannot be the same. The random number Array
1. Define a one-dimensional number with the array size of 24.
2. Generate 0 ~ A Random Number of 23.
3. Store the generated random number into the I array. Each data in the array must not be the same.
4. Note that this subroutine requires that the array contains 0 ~ 23.
5. Note that the C language has a random number function, which can be used to generate a random number. RAND (N)
# Include <stdio. h>
# Include <time. h>
# Define ArrayMaxItem 24
Int RandTest ()
{
Int I = 0;
Int ret = 0;
Time_t t;
For (I = 0; I <10; I ++)
{
Srand (unsigned) time (& t) * 10 );
Ret = rand ();
// Ret = rand (unsigned) time (& t ));
Printf ("ret: % d \ n", ret );
}
Return 0;
}
Int RandArray (int * a, int n)
{
Int I = 0;
Int t = 0;
Time_t mytime;
Struct tm * timeinfo;
For (I = 0; I <n; I ++)
A [I] = 0;
/* Only n-1 posions need to by seted */
For (I = 0; I <n-1; I ++)
{
While (a [t = rand (unsigned) time (& mytime) % n]! = 0 );
A [t] = n-i-1;
Timeinfo = localtime (& mytime );
Printf ("time: % s a [% d]: % d \ n", asctime (timeinfo), t, a [t]);
}
For (I = 0; I <n; I ++)
{
Printf ("a [% d]: % d \ n", I, a [I]);
}
Return 0;
}
Int main ()
{
Int a [ArrayMaxItem];
RandArray (a, ArrayMaxItem );
// RandTest ();
Return 0;
}
1. Use of random numbers:
On the Internet, we need to first call the srand (unsigned seed) function to set the random seed, and then call rand (void) to generate a random number, but I use the system time as the seed, the 10 random values are the same.
Then, the method used in the above code is used to directly pass the seed into the rand () function, and different values are generated after 10 calls, but each call produces the same group of values.
2. Obtain the system time:
# Include <stdio. h>
# Include <time. h>
Int main ()
{
Time_t rawtime;
Struct tm * timeinfo;
Time (& rawtime );
Timeinfo = localtime (& rawtime );
Printf ("The current date/time is: % s", asctime (timeinfo ));
Return 0;
}
Time_t // time type (defined by time. h)
Struct tm {// time structure, which is defined as follows:
Int tm_sec;
Int tm_min;
Int tm_hour;
Int tm_mday;
Int tm_mon;
Int tm_year;
Int tm_wday;
Int tm_yday;
Int tm_isdst;
}
Time (& rawtime); // obtain the time, in seconds, from January 1, January 1, 1970, stored in rawtime
Localtime (& rawtime); // convert to local time, tm Time Structure
Asctime () // convert to standard ASCII time format:
// The tm is printed directly. tm_year is calculated from January 1, 1900, and tm_mon is calculated from 0. Therefore, 1 is required.
3. Ideas:
Use a random number to generate 0 ~ N-1 range of array subscript, when the corresponding number is not 0, set to n-i-1, so, only need to set n-1, the last is 0.
Generate 0 ~ The random number in the n-1 range is used to obtain the remainder of the random number produced by rand.
4. Running result:
Time: Tue Apr 19 12:28:42 2016
A [7]: 23
Time: Tue Apr 19 12:28:42 2016
A [22]: 22
Time: Tue Apr 19 12:28:42 2016
A [9]: 21
Time: Tue Apr 19 12:28:42 2016
A [19]: 20
Time: Tue Apr 19 12:28:42 2016
A [17]: 19
Time: Tue Apr 19 12:28:42 2016
A [10]: 18
Time: Tue Apr 19 12:28:42 2016
A [12]: 17
Time: Tue Apr 19 12:28:42 2016
A [13]: 16
Time: Tue Apr 19 12:28:42 2016
A [2]: 15
Time: Tue Apr 19 12:28:42 2016
A [11]: 14
Time: Tue Apr 19 12:28:42 2016
A [18]: 13
Time: Tue Apr 19 12:28:42 2016
A [4]: 12
Time: Tue Apr 19 12:28:42 2016
A [16]: 11
Time: Tue Apr 19 12:28:42 2016
A [8]: 10
Time: Tue Apr 19 12:28:42 2016
A [15]: 9
Time: Tue Apr 19 12:28:42 2016
A [21]: 8
Time: Tue Apr 19 12:28:42 2016
A [14]: 7
Time: Tue Apr 19 12:28:42 2016
A [6]: 6
Time: Tue Apr 19 12:28:42 2016
A [5]: 5
Time: Tue Apr 19 12:28:42 2016
A [1]: 4
Time: Tue Apr 19 12:28:42 2016
A [23]: 3
Time: Tue Apr 19 12:28:42 2016
A [20]: 2
Time: Tue Apr 19 12:28:42 2016
A [3]: 1
A [0]: 0
A [1]: 4
A [2]: 15
A [3]: 1
A [4]: 12
A [5]: 5
A [6]: 6
A [7]: 23
A [8]: 10
A [9]: 21
A [10]: 18
A [11]: 14
A [12]: 17
A [13]: 16
A [14]: 7
A [15]: 9
A [16]: 11
A [17]: 19
A [18]: 13
A [19]: 20
A [20]: 2
A [21]: 8
A [22]: 22
A [23]: 3