C and C++ generate instance code that specifies range and variable range random Numbers

  • 2020-04-02 02:03:03
  • OfStack

Generate random Numbers without specifying a range
Using the function rand (), the function prototype is int ra
Nd (), no parameters. An integer between 0 and RAND_MAX is generated. The size of RAND_MAX can be viewed. In stdlib.h of the include folder (Linux in usr directory, Windows in installation directory), the value under Linux is 2147483647 (), which is system-specific.
Reference code:


#include<stdio.h>
#include<stdlib.h>
int main()
{
        int i;
        for(i=0; i<10; i++)  //I'm going to randomly generate 10 Numbers.
        {
            printf("%dn", rand());
        }
        return 0;
}

2. The specified range generates random Numbers, from 0 to a certain number

There's no function out there, but you can get it by taking a mod


#include<stdio.h>
#include<stdlib.h>
#define Random(x) (rand() % x) //Gets a random number in a specified range by taking a remainder
int main()
{
        int i;
        int dis;               //Generate random Numbers between [0, dis), note that dis is not included
        for(i=0; i<10; i++)
        {    
            printf("%dn", Random(dis));
        }
        return 0;
}

Let's say dis is 5
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/20131120110707.jpg? 20131020111422 ">

Note one problem: each execution of the above two programs produces the same result, which is a pseudorandom number. Random number generated by rand() is related to the specific seed. When the seed is not acquired with srand(), the default value of the seed is 1. Therefore, different seeds need to be generated with srand() function. 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:


#include<stdio.h>
#include<stdlib.h>
#include<time.h> 
int main()
{
        int i;
        srand((int)time(NULL));     //Different random Numbers are generated for each seed execution
        for(i=0; i<10; i++)
        {
          printf("%dn", rand());  //Because the execution is so fast, in less than a second, the 10 random Numbers are identical, but each execution is different
        } 
        return 0; 
}

Generate random Numbers within a specified range

Requirements: specify the range (m,n),m,n relations are uncertain, random Numbers including m and n
Somehow, change the range (m,n) to (0, X), and then move back. Three of the following


1 : m=n I shouldn't call it a random number at this point m
2 : m>n:
         tag pos=n The distance difference pos=m-n+1
      return  rand() % dis + pos
3 : n>m:
         tag pos=m The distance difference =n-m+1
      return rand()%dis + pos

Reference code:


#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("%dn", 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


Related articles: