田田她老爹  

前几天闺女给自己出了一个小题目,用户登录,输入验证码,修改密码。这里涉及到随机产生数字验证码。下面写写rand()的学习笔记。 rand()及srand() 这两个函数是标准库函数

先写个小例子看看

 1 #include <iostream>
 2 #include <cstdlib> 
 3 using namespace std;
 4 int main() {
 5     int r;
 6     r=rand();
 7     cout << "r="<<r;
 8     
 9 
10     return 0;
11 }

编译运行一下吧。

r=41

老有成就感吧

在运行一遍

r=41

怎么还是41再试试运行

r=41

不会吧不是随机么怎么只随出41了。来看看下面这段

 1 #include <iostream>
 2 #include <cstdlib> 
 3 using namespace std;
 4 int main() {
 5     int r1;
 6     
 7     r1=rand();
 8     
 9     cout << "r1-1="<<r1<<endl;
10     
11     r1=rand();
12     
13     cout << "r1-2="<<r1<<endl;
14     
15     return 0;
16 }

 结果呢,看下

r1-1=41
r1-2=18467

再来一次

r1-1=41
r1-2=18467

虽然r1的随机值在两次赋值后有了不同变化,但每次运行结果都是相同的,这个随机还有些恋旧哦。

 来看看reference是如何介绍的

http://www.cplusplus.com/reference/cstdlib/rand/

int rand (void);
Generate random number

Returns a pseudo-random integral number in the range between 0 and RAND_MAX.

This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using function srand.

rand()范围确实是一个0~RAND_MAX.的一个整数 ,这个RAND_MAX一会儿再说,但要说的是这里产生的是pseudo-random(伪随机数)伪随机数又是什么呢,扯得越来越远了,真正的随机是真正意义的不可预见,而计算机产生的随机数一定是通过算法模拟产生的,结果也是确定可见的。只是让我们看起来随机罢了。所以在算法固定,其他条件也相同的条件下,您老人运行多少次,结果也必然是相同的。

rand_max 就是一个数值最小是32767

http://www.cplusplus.com/reference/cstdlib/RAND_MAX/

那如何模拟的更像随机数呢? srand()是有些作用的,我们再看下srand()

 

http://www.cplusplus.com/reference/cstdlib/srand/

void srand (unsigned int seed);
Initialize random number generator

The pseudo-random number generator is initialized using the argument passed as seed.

For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand.

Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand.

If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to randor srand.

In order to generate random-like numbers, srand is usually initialized to some distinctive runtime value, like the value returned by function time (declared in header <ctime>). This is distinctive enough for most trivial randomization needs.

 也就是说srand()会根据seed这个种子,让rand()去产生不同的随机数,如果seed相同那rand()产生的随机数也就会相同。

做个实验验证一下

#include <iostream>
#include <cstdlib> 
using namespace std;
int main() {
    int r1;
    srand(1); 
    r1=rand();
    
    cout << "r1="<<r1<<endl;
    
    
    return 0;
}

运行一下看看

r1=41

好奇怪,怎么也得41,再试试。

r1=41

难道和41干上了,来看看这段就明白了

Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand.

If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to randor srand.

看到了么 srand()初始值是1,也就是说在没有srand(seed) rand()在调用时会先去调用srand(1).所以你rand() 只能看到41.

我们只要让每次这个seed不同就可以产生随机值了。那如何才能不同呢?找个seed,时间是不会重复的。

#include <iostream>
#include <cstdlib>
#include <ctime> 
using namespace std;
int main() {
    int r1;
    int t=time(NULL);
    srand(t); 
    r1=rand();
    
    cout << "r1="<<r1<<endl;
    cout << "time=" << t <<endl;
    
    return 0;
}

运行3遍

r1=24077
time=1585315948
r1=24201
time=1585315986
r1=24266
time=1585316006

产生了不同随机数 下面那个time是时间戳,从1970-01-01到现在的时间以秒为单位。

这就是一个最最简单的随机数生成代码,如何控制范围 小盆友自己动脑子吧。

posted on 2020-03-27 21:48  田田她老爹  阅读(633)  评论(0)    收藏  举报