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 random number rand () % N range is obviously 0 ~ N-1;

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

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

In this way, we have no analysis on the seeds and floating-point numbers,

 

The following describes how to use rand (). The floating point number should be placed at the end of the lecture: Generally, a seed should be initialized before this is used, but if you do not write it, the system will give you a default seed. below is the code for inputting the seed by ourselves;

 

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 each 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 than runtime.

Use the following code as an example to make it more appropriate than compile:

#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 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 we want to be more accurate? What about 0.11 ??? The truth is the same.

We are able to output (RAND () % N)/(n * 1.0) + (RAND () % N)/(n * 10.0 );

Thus, we can conclude that p represents the exact number of digits.

{P}

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

 

C random C ++ rand function application

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.