C + + Generates instance code for specified range and indefinite range random number _c language

Source: Internet
Author: User
Tags function prototype rand

I. Generating random numbers without a specified range
With the function rand (), the function prototype is int RA
nd (), no parameters. A whole number between 0~rand_max is generated at this time. The size of the Rand_max can be viewed in the Include folder (Linux in the USR directory, windows in the installation directory) Stdlib.h can be seen, Linux under the value of 2147483647 (), the value of the specific system.
Reference code:

Copy Code code as follows:

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

int main ()
{
int i;
For (i=0 i<10; i++)//randomly generates 10 numbers.
{
printf ("%d\n", Rand ());
}
return 0;
}

Two. The specified range produces random numbers, generating 0 to a certain number of random numbers

There are no ready-made functions, but they can be obtained by

Copy Code code as follows:

#include <stdio.h>
#include <stdlib.h>
#define RANDOM (x) (rand ()% x)//Get the random number of the specified range by taking the remainder
int main ()
{
int i;
int dis; Generates a random number between [0, dis), noting that DIS is not included
for (i=0; i<10; i++)
{
printf ("%d\n", Random (dis));
}
return 0;
}

Description: Suppose dis takes 5

Note A problem: each of the above two programs produces the same results, both pseudo random numbers. Rand () produces a random number associated with a specific seed, and when the seed is not deliberately fetched with srand (), the seed defaults to 1, so the Srand () function is required to produce a different seed, Srand function prototype: void Srand (unsigned seed); In order to produce different seed values, time is usually used as the parameter value. For example, for one, the modification procedure is as follows:

Copy Code code as follows:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int i;
Srand ((int) time (NULL)); Generate different random numbers each time the seed is executed differently
for (i=0; i<10; i++)
{
printf ("%d\n", Rand ()); Because execution is too fast, in less than a second, 10 random numbers are interlinked, but each execution is different
}
return 0;
}

Three. The specified range produces random numbers

Requirements: Specified range (m,n), M, n uncertain, random number including M and n
Find ways to change the scope (m,n) to (0,x), and then transfer back to the end. Three kinds of cases

Copy Code code as follows:

1:m=n at this time should not be called random number, here return m
2:m>n:
Mark Pos=n, Distance difference pos=m-n+1
Back to rand ()% dis + pos

3:n>m:
Mark Pos=m, Distance difference =n-m+1
Back to Rand ()%dis + pos

Reference code:

Copy Code code as follows:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int Random (int m, int n)
{
int pos, dis;
if (M = = N)
{
return m;
}
else if (M > N)
{
pos = n;
dis = m-n + 1;
Return rand ()% dis + pos;
}
Else
{
pos = m;
dis = n-m + 1;
Return rand ()% dis + pos;
}
}
int main ()
{
int I, M, N;
Srand ((int) time (NULL));
m =-3;
n =-7;
for (i=0; i<10; i++)
{
printf ("%d\n", Random (M, n));
}
return 0;
}

Sublimation
Srand ((unsigned) time (null));
(A,B) (rand ()% (b-a+1)) +a-1
[A,b) (rand ()% (b-a)) +a
(A,b] (rand ()% (b-a)) +a+1
[A,b] (rand ()% (b-a+1)) +a

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.