首页 > 编程知识 正文

随机数函数rand,random.randn函数用法

时间:2023-05-05 00:58:31 阅读:224810 作者:4464

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 99v2 = rand() % 100 + 1; // v2 in the range 1 to 100v3 = 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:%dn", 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 10int 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: %dn", rand()%100); srand (time(NULL)); printf ("Random number: %dn", rand()%100);//每次随机不一样 srand (1); printf ("Again the first number: %dn", rand()%100); srand (1); printf ("Again the first number: %dn", rand()%100); printf ("Again the first number: %dn", rand()%100); return 0;}

输出:

First number: 83Random number: 81Again the first number: 83Again the first number: 83Again 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_enginemersenne_twister_enginesubtract_with_carry_enginediscard_block_engineindependent_bits_engineshuffle_order_enginedefault_random_engineminstd_randminstd_rand0mt19937mt19937_64ranlux24_baseranlux48_baseranlux24ranlux48knuth_brandom_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:pbdmj分布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/

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。