随机函数rand()、srand()与random的用法

rand()

随机函数rand()的头文件为<cstdlib> (stdlib.h)。rand()不需要参数,它会返回一个从0到最大随机数(RAND_MAX)的任意整数

说明:

  • 0到99:int num = rand() % 100;
  • 1-100,则是这样:int num = rand() % 100 + 1;
  • a到b之间的随机整数:rand() % (b-a+1)+ a ;
  • 若要产生0~1之间的小数,则可以先取得0~10的整数,然后均除以10即可得到“随机到十分位”的10个随机小数。若要得到“随机到百分位”的随机小数,则需要先得到0~100的10个整数,然后均除以100,其它情况依 此类推。
  • 通常rand()产生的随机数在每次运行的时候都是与上一次相同的,这样是为了便于程序的调试。
v1 = rand() % 100;         // v1 in the range 0 to 99
v2 = rand() % 100 + 1;     // v2 in the range 1 to 100
v3 = rand() % 30 + 1985;   // v3 in the range 1985-2014 

下面的代码为猜测随机生成的代码:

#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

int main ()
{
    
    
  int iSecret, iGuess;

  /* initialize random seed: */
  srand (time(NULL));//为了每次运行程序时(重启程序),生成的随机数不一样

  iSecret = rand() % 10 + 1;//1-10
  printf ("iSecret:%d\n", iSecret);

  do {
    
    
    printf ("Guess the number (1 to 10): ");
    scanf ("%d",&iGuess);
    if (iSecret<iGuess) puts ("The secret number is lower");
    else if (iSecret>iGuess) puts ("The secret number is higher");
  } while (iSecret!=iGuess);

  puts ("Congratulations!");
  return 0;
}

使用while循环测试,每次生成的随机数不一样:

#include<iostream>
#include <stdlib.h>     /* srand, rand */
int main ()
{
    
    
    while(1)
    {
    
    
        int rand_num = rand()%10 +1;//每次生成的随机数不一样
        std::cout<<rand_num<<std::endl;
    }
    return 0;
}

使用for循环产生的随机数也不一样:

#include <iostream>
using namespace std;
#include   <stdlib.h>
#include   <time.h>
#define MIN 1    //随机数产生的范围
#define MAX 10

int main()
{
    
    
    int i;
    srand((unsigned)time(0));
    cout<<"Ten random numbers from "<<MIN<<
          " to "<<MAX<<" :\n"<<endl;
    for(i=0; i<10; i++)          //产生随机数
    {
    
    
        cout<<MIN + (int)MAX * rand() / (RAND_MAX + 1)<<"; ";
    }
    cout<<endl;
    return   0;
}

输出:

Ten random numbers from 1 to 10 :
9; 4; 5; 5; 9; 4; 3; 10; 10; 7; 

srand()

初始化随机数发生器srand()的头文件和rand()一样。srand()用来设置rand()产生随机数时的随机数种子。如果没有设置,默认种子就是1,则每次产生的随机数都是一样的。

#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

int main ()
{
    
    
    printf ("First number: %d\n", rand()%100);
    srand (time(NULL));
    printf ("Random number: %d\n", rand()%100);//每次随机不一样
    srand (1);
    printf ("Again the first number: %d\n", rand()%100);
    srand (1);
    printf ("Again the first number: %d\n", rand()%100);
    printf ("Again the first number: %d\n", rand()%100);

    return 0;
}

输出:

First number: 83
Random number: 81
Again the first number: 83
Again the first number: 83
Again the first number: 86

random

random是C++11提供的一个头文件,是一个随机数生成工具。这个库允许使用生成器和分布的组合产生随机数。

生成器(Generators):均匀分布的数字对象
分布器(Distributions): 将生成器生成的数字序列转换为遵循特定随机变量分布的数字序列的对象,例如均匀,正态或二项式。

下面是一个简单的生成器和分布器的使用例子:

#include<iostream>
#include<random>
int main()
{
    
    
    std::default_random_engine generator;//生成器
    std::uniform_int_distribution<int> distribution(1,6);//分布器
    int dice_roll = distribution(generator);  // generates number in the range 1..6
    std::cout<<dice_roll<<std::endl;
}

生成器和分布器有很多不同的版本。

生成器(Generators):

  • linear_congruential_engine
  • mersenne_twister_engine
  • subtract_with_carry_engine
  • discard_block_engine
  • independent_bits_engine
  • shuffle_order_engine
  • default_random_engine
  • minstd_rand
  • minstd_rand0
  • mt19937
  • mt19937_64
  • ranlux24_base
  • ranlux48_base
  • ranlux24
  • ranlux48
  • knuth_b
  • random_device

分布器(Distributions)

  • uniform_int_distribution:均匀离散分布
  • uniform_real_distribution:均匀实数分布
  • bernoulli_distribution:伯努利分布
  • binomial_distribution:二项式分布
  • geometric_distribution:几何分布
  • negative_binomial_distribution:负二项式分布
  • poisson_distribution:泊松分布
  • exponential_distribution:指数分布
  • gamma_distribution:伽玛分布
  • weibull_distribution:威布尔分布
  • extreme_value_distribution:极值分配
  • normal_distribution:正态分布
  • lognormal_distribution:对数正态分布
  • chi_squared_distribution:卡方分布
  • cauchy_distribution:柯西分布
  • fisher_f_distribution:Fisher F分布
  • student_t_distribution:学生T分布
  • discrete_distribution:离散分布(类模板)
  • piecewise_constant_distribution:分段常数分布
  • piecewise_linear_distribution:分段线性分布

详细使用可参考官网random教程

参考:
http://www.cplusplus.com/reference/cstdlib/rand/
http://www.cplusplus.com/reference/cstdlib/srand/
http://www.cplusplus.com/reference/random/

猜你喜欢

转载自blog.csdn.net/QLeelq/article/details/113563383
今日推荐