C random C ++ rand function application

Source: Internet
Author: User

Random functions are not ansi c standards and cannot be compiled by GCC, Vc, or other compilers. But in C Language int random (Num) can be used like this, it returns a random number from 0 to num-1.

You can use the rand function in C ++.

Rand () % N range 0 ~ N-1

Rand () is mainly used to generate random numbers. Others can be ignored here.

Obviously, the range of any number rand () % N is obviously 0 ~ N-1;

How to generate n ~ What about the number of M? In the same way, we only need to perform some symbolic operations on rand;

N + rand () % (m-n + 1 );

In this way, we can only analyze the seeds and floating-point numbers,

 

Next, let's talk about the usage of rand (). The floating point number is put at the end. Generally, a seed should be initialized before it is used. If you do not write it, the system will give you a default seed, below is the sample code we enter;

 

int seed;scanf ("%d",&seed);srand(seed);cout<<rand()<<endl;

 

#include <stdio.h>#include <stdlib.h>#include <time.h>int main(){    int arr[15];    //srand(time(NULL));    int seed;    while(1){    scanf("%d",&seed);    srand(seed);    for (int i=0; i<15; i++)         printf ("%d\t",rand()%10);    printf ("\n");      }    return 0;}

After comparison, it is found that every seed is a random variable value that maintains this State and will exist in the system;

Therefore, we need to keep the time difference for this initialization seed; that is to say, we still use srand (Time (null); better

The following code is suitable:

#include <stdio.h>#include <stdlib.h>#include <time.h>int main(){    //int arr[15];    srand(time(NULL)); for (int i=0; i<15; i++)         printf ("%d\t",rand()%10);    printf ("\n");      while (1);    return 0;}

Now let's talk about the last point -- random generation of floating point numbers.

Rand () % N ============= 0 ~ N-1, then we can divide it by N.

It can be expressed as: (RAND () % N)/(n * 1.0) // here note that the implicit conversion is low ------> high

The following is an example:

#include <stdio.h>#include <stdlib.h>#include <time.h>int main(){    int arr[15];    //srand(time(NULL));    int seed;    while(1){    scanf("%d",&seed);    srand(seed);    for (int i=0; i<15; i++)         printf ("%lf\t",(rand()%10)/10.0);    printf ("\n");      }    return 0;}

 

 

What if it is more accurate? What about 0.11 ??? The truth is the same.

We can output (RAND () % N)/(n * 1.0) + (RAND () % N)/(n * 10.0 );

Thus, we can sum up the rule that p represents the exact number of digits.

{P}

1 ...... 1 * (RAND () % N)/10 ^ P

 

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.