The function and delay function of generating random numbers in C language

Source: Internet
Author: User
Tags printf

The C language code below uses a function that generates random numbers, and a delay function. Please carefully observe its display effect.

From the following code, we can draw an important conclusion: when the above two kinds of functions are put into the loop, we should make some changes. Also focus on the definition of its parameters (outside of the child function?). Inside the child function? Global variable? Local variables? )。

Also note: scanf should use%LF when typing a double type, but only use%f when printf.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

Double T_delay;
time_t start,end; Start,end the modified position

int Createornot ()
{

int flag;
Srand (NULL);//srand () Original position
flag = (int) ((2*rand ()/rand_max) +1);

Return Flag;//return 1 or 2
1 create,2 not create.

}

void Delay ()
{
time_t Start,end;//end the original position
Double dif;
Time (&start);
while ((dif= difftime (End,start)) <5000.0)//notice here
{
Time (&end);

};

}

void delay (time_t end)//end modified location
{
time_t start;//,end;
Double dif;
Time (&start);
while ((dif= difftime (End,start)) <2.0)//notice here
{
Time (&end);

};

}

void Delay (time_t end,double time_delay)//change ...
{

time_t Start;//,end;//change ...
Double dif;
Time (&start);
Time (&end);

while ((dif= difftime (End,start)) < Time_delay)
{

Time (&end);

};

}

void delay (time_t start,time_t end)//change ...
{

time_t Start;//,end;//change ...
Double dif;

Time (&start);
Time (&end);

while ((dif= difftime (End,start)) < T_delay)
{

Time (&end);

};

}

int main ()
{

int I,flag = 1,times = 10;

time_t start,end; Start,end the modified position
Srand (Time (NULL)), and/or corrected, Srand () where it should be.

for (i=0;i<times;i++)
{

Flag= Createornot ();
printf ("%d\n", flag);
Delay ();
Delay (end);
}

printf ("\ n \ nthe");

for (i=0;i<times;i++)
{

Flag= Createornot ();
printf ("%d\n", flag);
Delay ();
Delay (end);
}

printf ("t_delay:\n");
scanf ("%lf", &t_delay);/////////////////////////////////////
printf ("t_delay:%f\n", T_delay);
for (i=0;i<times;i++)
{

Flag= Createornot ();
printf ("%d\n", flag);
Delay ();
Delay (End,t_delay);
}

printf ("t_delay:\n");
scanf ("%lf", &t_delay);

for (i=0;i<times;i++)
{

Flag= Createornot ();
printf ("%d\n", flag);
Delay ();
Delay (start,end);
}

}

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.