A detailed explanation of the use of the RAND function in C language _c language

Source: Internet
Author: User
Tags current time rand

Objective

In the process of programming the algorithm, we often need to use random numbers. Since the computer is a logically based machine, it is impossible to do a real random (probably quantum computer?). )。 So the computer generates pseudo random numbers for us to use.

We use the RAND function of the C language, which is also a pseudo random number.

The use of the RAND function in C language

1. Write header file

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

2, the definition of the variable

void Main (void) {int i,k;

3, Srand ((unsigned) time (NULL)); /* Select Seed File/* *

4, for (i = 0; I <20;i++)/* Loop control 20 random number of generation/{K=rand ()%100;/* Store random number/printf ("k=%d\n", k); * Output random number *}}

1, this is a random function of a generation method

2, if only one, then can omit circular control

A simple demonstration is as follows:

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

int
main (int argc, char* * argv)
{
  //To construct the "seed" of the pseudo random number in the current time of the machine. 
  Srand ((unsigned int) time (NULL));
  int i;
  Print 10 pseudo random numbers 
  for (i = 0; i < i++) {
    
    printf ("%d", Rand ());
  }
  printf ("\ n");
  
  System ("pause");
  return 0;
}

Summarize

The above is about the use of the RAND function in C language all the content, I hope this article on the daily use of C language can help.

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.