VC random function Srand and Rand usage _c language

Source: Internet
Author: User
Tags current time rand random seed

In programming, it is often necessary to use random numbers, of course, the program (function) itself can not generate so-called real random number, but also need a random seed, and then according to the established algorithm to calculate a definite result. If we only need a finite time or a random number of times to require a longer interval, we can also use the current time (timestamp) as a random number. Since time can be a random number, why do we need a special random function? This involves the probability distribution of random sequence, and it can be ensured that the sequential random number sequence is as close as possible to a uniform and reasonable probability distribution by specially designed random function. And not like the open color, the leadership of a few open a few, so the repetition probability is too high.

The most common use of random functions in VC is Srand and rand (which is actually a standard C function), where srand is responsible for setting up random seeds, and Rand is responsible for generating random numbers. Use this two random function to include a <stdlib.h> header file.

First, Srand and Rand functions

1. Srand function
The Srand definition is as follows: void srand (unsigned int seed);
Where seed is an unsigned integer, we generally use the timestamp as its parameter, get the timestamp and need to include the <time.h> header file. Apply the sample parameters below.
2. Rand function
The rand () function produces a pseudo-random integer (short) between 0~rand_max, RAND_MAX macro defined as 0X7FFF. If a larger number of random integers is required, multiple random integers can be used for combinatorial operations.

Second, random number generation example

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

void main (void)
{
 int i,r;
 Srand ((unsigned) time (NULL));

 for (i = 0;i < 10;i++) {
  r=rand ()%10;
  printf ("%3d\n", R);
 }
}

Use Note:
1. Rand generation sequence for a given srand seed, the sequence value combination is fixed
2, Srand value in a quick call, time (NULL) may not be updated, because the timestamp is computed in microseconds, so if you execute Srand and Rand again within microseconds, you get the same sequence of random values.
3. Each time a random seed is set, the rand output is automatically reset to the first initial value, the same seed, and the initial and subsequent sequences are the same
Examples are as follows:

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

int foo () {
	int r;
	Srand ((unsigned) time (NULL));
	r = rand ()%100;
	return r;
}

void Main (void) {
	int i,r;

	Srand ((unsigned) time (NULL));
	for (i = 0; i < 10;i++) {
		r=rand ()%100;
		printf ("%3d", R);
	}
	printf ("\ n");

	for (i=0 i<10;i++) {
		printf ("%3d", Foo ());
	}
	printf ("\ n");
}

The output is as follows (note that the second row has exactly the same output):

76 69 10 95 37 85 25 99 4 33

76 76 76 76 76 76 76 76 76 76

Resources:

http://msdn.microsoft.com/en-us/library/aa272944 (v=vs.60). aspx

http://msdn.microsoft.com/en-us/library/aa272875 (v=vs.60). aspx

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.