Application of random function in C language

Source: Internet
Author: User
Tags function prototype printf rand random seed

Maybe everyone knows the random function random in C, but the random function is not ANSI C standard, so the random function cannot be compiled and passed by compiler such as GCC,VC. So how to implement the random function in C language?

In addition to the random function, there is a Rand function, also a random function, that can produce random numbers from 0 to Rand_max.

#include <stdio.h>
#include <stdlib.h>
int main()
{
int k;
k=rand();
printf("%d\n", k);
return 0;
}

We can compile the above code to run, and found that he did produce random numbers, but you will find that each run the program generated random numbers are the same, why? Because random numbers take a fixed sequence in the C language, each execution takes the same number.

So how do you write a program that produces random numbers that are different every time you run it? Take a look at the following example:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int i;
time_t t;
srand((unsigned) time(&t));
printf("Ten random numbers from 0 to 99\n\n");
for (i=0; i<10; i++)
printf("%d\n", rand()%100);
return 0;
}

When you run the program, you will find that the random numbers are different every time you produce them.

So why is the first program the same and the second program is different?

The second program uses a new function srand, which produces a random seed (seed) for random numbers, and the function prototype is Srand ((unsigned) time (NULL));

The value of time is different every moment. So the seeds are different, so the random numbers that are produced are different.

So, to produce a different random number, you need to call Srand before you can use Rand.

Since Rand produces random numbers from 0 to Rand_max, and Rand_max is a very large number, how do you generate numbers from X~y?

From X to Y, there are y-x+1 numbers, so to produce numbers from X to Y, you only need to write this:

K=rand ()% (y-x+1) +x;

In this way, you can generate random numbers in any range you want.

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.