The RAND function and the Srand function in C

Source: Internet
Author: User
Tags define function

Rand function and Srand function


First we have a general view of Rand&srand: Srand initializes random seeds, Rand generates random numbers, which are explained in detail below.

1. Rand (generate random number)

Table header files: #include <stdlib.h>

define function: int rand (void)

Function Description:

Because Rand's internal implementation is done linearly with congruential, he is not really a random number, only because of its exceptionally long period, so that a certain range can be seen as random, and Rand () returns a random number, ranging between 0 and Rand_max. Before calling this function to produce a random number, the random number seed must be set with Srand (), and if no random number seed is set, rand () automatically sets the random number seed to 1 when called. Rand () produces false random numbers that are the same each time they are executed. To be different, initialize it with a different value. The Initialized function is Srand ().

return value:

Returns a random integer value between 0 and Rand_max, with a range of Rand_max at least between 32767 (int), or double-byte (16-digit). If the unsigned int double byte is 65535, four bytes is the integer range of 4294967295.

0~rand_max the odds of each number being selected are the same.


2, Srand (set random number Seed)

Table header files: #include <stdlib.h>

Define function: void srand (unsigned int seed);

Function Description:

The Srand () is used to set the random number seed when rand () produces a random number. The parameter seed must be an integer and can usually be used as seed using the return value of Geypid () or time (0). If each seed is set to the same value, rand () produces a random number that is the same every time.


code example: (used to generate a random number between 20 and 0-100, if you need to produce a number of n~m?) We just have to do some symbolic operations on rand ()N+rand ()% (m-n+1); Which generates a random number between M-n plus n)

/crt_rand.c//The seeds the Random-number generator//with the time and then displays random integers.//#include <stdlib.h> #include <stdio.h> #include <time.h>int main (void) {    int i;      Seed the Random-number generator with current time so and//the numbers'll be    different every time we run.    Srand ((unsigned) time (NULL));    Display ten numbers.    for (i = 0;    I < 20;i++)       printf ("   %6d\n", Rand ());   printf ("\ n");   Usually, you'll want to generate a number in a specific range,   //such as 0 to +, like this:   {      int R ange_min = 0;      int range_max = +;      for (i = 0;     I < 20; i++)       {          int rand100 = ((double) rand ()/                         (double) rand_max) * (range_max-range_min) + range_min);          printf ("   %6d\n", rand100);       }   }



The difference between Ps:rand () and random ():

int rand (void): Returns an int type integer between 0------Rand_max, which is a non-thread-safe function. And the performance of generating random numbers is not very good, it is deprecated

long int random (void): Returns a long integer between 0-------Rand_max that produces a very large random value up to 16* ((2**31)-1). The random function generates a continuous pseudo-random number returned by a default size of 31 long integer tables using a nonlinear feedback stochastic number generator.

If you are planting seeds using srandom, then you should use random to return the numbers, and if you use srand to grow the seeds, you should use Rand to return random numbers.

Srand and RAND officials have not recommended it. The reason is that the performance of random numbers is not very good, and the randomness of random numbers is not good, and the other is not thread safety.




The RAND function and the Srand function in C

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.