C language will generate a random number into an array, the data can not be the same

Source: Internet
Author: User
Tags local time

1. Define a one-dimensional number with an array size of 24.

2. Generate a random number of 0~23.

3. The resulting random number is stored in the I array, requiring that each data in the array cannot be the same.

4. Add that the subroutine requires that the 0~23 data be stored in the array after each invocation, and that the data is not duplicated.

5. Note that the C language has a random number function, which can be used to generate random numbers. 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 the random number process:

The internet says to call the Srand (unsigned seed) function to set the random number seed, and then call rand (void) to produce a random number, but I use the system time as a seed, generated 10 random values are the same.

Following the method used in the above code, the seed is passed directly into the rand () function, and 10 calls are made to produce different values, but each call produces the same set of values .

2. Get 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 (time.h definition)
struct TM {//time structure, time.h 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); Acquisition time, in seconds, from January 1, 1970, stored in Rawtime
LocalTime (&rawtime); Convert to local time, TM time structure
Asctime ()//convert to standard ASCII time format:
is direct printing tm,tm_year from 1900, so to add 1900, month Tm_mon, calculated from 0, so add 1

3. Ideas:

Using random numbers to generate an array subscript in the 0~n-1 range, when the corresponding number is not 0, then set to N-i-1, so that only need to put n-1, the last one is 0.

The method for generating a random number in the 0~n-1 range is to take the random number pair n to generate the rand ().

4. Operation 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

C language will generate a random number into an array, the data can not be the same

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.