An introductory course for generating random numbers in C programming language _c language

Source: Internet
Author: User
Tags function prototype rand

Language generation random number is a common programming function task, of course, this is not difficult, call two or three functions come out, but you know how these functions specifically play a role, and how they produce random numbers?

Several concepts

Random number: In mathematics, all the pseudo random numbers are generated, and the real random numbers are generated by physical methods.

Random number seed: the generation of random numbers is produced by arithmetic rules, the random number of seeds of srand (seed) is different, and rand () is different, and the value of rand () is the same if the random number of seeds is the same each time. Therefore, to produce random numbers, the random number of srand (seed) must also be random.

To produce a random number of seeds with Srand ()

Prototype: void Srand (unsigned int seed);

The effect is to set the random number of seeds, in order to make the random number of seeds is random, usually with time (NULL) value of the seed.


Time () for random number of seeds

Function prototype: time_t time (time_t * timer);

The time () function represents the number of seconds to return from 1970-1-1 00:00:00 to the current date.

When used: Srand (unsigned (NULL)), for example, to generate random integers between 1~10

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

int main ()
{
  Srand (Time (NULL));
  for (int i=0;i < 10;i++)
  {
    int randvalue=rand ()%10;
  }
}

The above program should note that Srand is outside the for loop, and if you put the srand in the For loop, the random numbers are the same each time.

Generate random numbers with Rand ()

Prototype: int rand (void);

The function is to produce a random number, of course the random number is a range, for 0~rand_max, random number and random number of seeds. Specifically, it does this when the random number of rand () is invoked:

If the user had previously called srand (seed), he would call again Srand (seed) to produce a random number of seeds;
If Srand (seed) is found to have not been invoked, Srand (1) is invoked automatically.
If the seed of random numbers generated by the call to Srand (seed) is the same (that is, the seed value is the same), Rand () produces the same random number.
Therefore, if you want Rand () to produce a different value each time it is invoked, you need to call Srand (seed) One at a time, and seed cannot be the same. Here is the reason why you often use time (NULL) to produce random numbers of seeds.

The rand () function can be used in C language to generate a uniform distribution from 0 to Rand_max. Based on this function, we can construct some specific random number generators to meet our needs.
(i) Distribution of uniform from 0 to 1:

Generate a random number in the range of [0,1]
double Uniform_zero_to_one () {return
  (double) rand ()/rand_max;< c4/>}

(ii) Uniform distribution of arbitrary real number intervals:

Generate a random real number in [Start,end]
double uniform_real (double start,double end) {
  double rate= (double ) rand ()/rand_max;
  Return start+ (End-start) *rate
}

(iii) Uniform distributions of arbitrary integer intervals:

Generate a random integer number in [start,end]
int uniform_integer (int start,int end) {
  int base=rand ();
  if (Base==rand_max) return
    Uniform_integer (start,end);
  int Range=end-start;
  int remainder=rand_max%range;
  int bucket=rand_max/range;
  if (Base<rand_max-remainder) return
    start+base/bucket;
  else return
    Uniform_integer (start,end);
}

This function should specifically illustrate that we normally use RAND ()%n to generate random numbers from 0 to n-1, but the distributions generated by this method are not uniform, and since Rand_max is only 32767, there is a need for additional ways to generate more random numbers than this number. Theoretically, the uniform distribution can be directly reduced from 0 to 1, but the actual effect is not good. This gives a way to achieve the shift.

(d) Random number of 32bits

Generate a random bits integer number 
int rand32 () {return
  (rand () <<16) + (rand () <<1) +rand ()%2 );
}

With a 32bits random number generation method, you can construct a random integer range in the 32bits range, as in the case of 16bits.

(v) Random integer intervals within 32bits range

Generate a random 32bits integer number in [start,end]
 int uniform_integer_32 (int start,int end) {
   int base=rand ();
  if (Base==rand32_max) return
    uniform_integer_32 (start,end);
  int Range=end-start;
  int remainder=rand32_max%range;
  int bucket=rand32_max/range;
  if (Base<rand32_max-remainder) return
    start+base/bucket;
  else return
    uniform_integer_32 (start,end);
}

Here Rand32_max is defined as 0x7fffffff.

In addition, the use of the rand () function to construct arbitrary distributed random numbers is also a problem worth discussing.

Theoretically, it can be obtained by the uniform distribution of (0,1), plus the standard sampling method (sampling).

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.