How to generate random numbers using Srand () and random number functions rand () in C language

Source: Internet
Author: User
Tags random seed

Excerpt from: Baidu Encyclopedia

The prototypes of these two functions in stdlib.h are:

int rand (); void srand (unsigned int);

? in the standard C library, the function RAND () can generate a random number between 0~rand_max, where Rand_max is an integer defined in Stdlib.h, which is related to the system. In order for the program to generate random values for a new sequence each time it executes, we usually provide a new random seed for the random number generator. the function srand () can spread the seed for the random number generator. as long as the seed is different, the rand () function produces a different sequence of random numbers. Srand () is called the initializer of the random number generator.

Usage:

Srand (Time (NULL)); randnum = rand ();

So Randnum is a real random number.

rand () generates pseudo-random numbers, srand functions provide seeds , and seeds produce different sequences of random numbers, so the Srand function is usually called first time () ( from Time.h Returns the time of the system (from Midnight 1970.1.1), in seconds, the same probability that the seed will produce the same number of random numbers is very small.


How to generate random numbers using Srand () and random number functions rand () in C language

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.