The rand () and random () functions in C/

Source: Internet
Author: User
Tags pow

NT rand (void);

Returns a random number 0 ~ Pow (2, sizeof (int))-1
long int random (void);

Returns a random number 0 ~ Pow (2, sizeof (long int))-1

void Srand (unsigned int seed);

Set the seed of the RAND function
void srandom (unsigned int seed);

Set the seed of the random function

Attention:
The difference between 1.rand and random is that the return type is different, int and long int (although the effect is the same on a normal 32-bit machine)
2. Before using Rand or random, use time (0) Getpid () to set random seeds. Otherwise, the default seed is 1, and the random number generated is the same.
3.rand (void) and srand (unsigned int seed) are used in conjunction with random (void) and srandom (unsigned int seed).


Ps:
The probability of generating a repeating random number using Rand or random is as follows:
100,000-----of 3
1 million-----of 230
10 million----of 23,000
Thus, the probability of repetition as the number increases is becoming more and more rapid.

The rand () and random () functions in C/

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.