关于loadrunner中rand()函数的使用
有两个问题请教大家:1. srand(time(NULL));
我不明白这个函数和随机取值有什么关系。又和当前时间有啥关系。最好能介绍一下工作原理。
2.请教大家rand()的取值范围:
例如: i=rand()%6+15, 取值范围包括15和21吗?还是只有16,17,18,19,20.
谢谢 1初始化随机
2.15-21 回复 1# zhangchaoy
rand()函数是生成一个随机的非负整数函数。
Example 1
The following example generates a random number restricted to a specific zero-based range using the mod (%) operator.
//srand is called before rand
srand(time(NULL));
/* Generate a random number from 0-99 */
lr_output_message
("A number between 0 and 99: %d\n", rand() % 100);
Output:
Action.c(7): A number between 0 and 99: 72
Example 2
The following example generates a random number, rNum, between 20 and 29 using the mod (%) operator and addition.
int rNum;
//srand is called before rand
srand(time(NULL));
/* The order of evaluation is first rand(), then the modulus,
then the addition. The command below is equivalent to :
rNum = rand() % 10;
rNum is now a number between 0-9
rNum += 20;
rNum is now between 20 and 29
*/
rNum= rand() % 10 + 20;
你的i=rand()%6+15返回的是15-21 谢谢两位了。 15-20 :handshake :)有用,赞! :victory: :victory: 没看懂 - - 15-20,云层叔叔误导人啊 确实是15-20 15-20,云层叔叔误导人啊
cu_zhuang 发表于 3/3/2012 17:12 http://bbs.51testing.com/images/common/back.gif
:D 云层老师开小差了肯定,没这么算术 的、:D、、 错,是我当做用rand()*数字了,这里用的是%取余 不明白,rand()*数字取的是整数还是四舍五入整数? 15-20
srand是随机种子生成,
接下来在使用rand,
相同2次随机数概率降低。 srand是始随机种子的,srand(time(NULL))进行初始化,
根据公式:rand()%(max—min+1)+min,算一下就知道是15~20啦,呵呵 回复 3# linlinxu
你开小差了……
/* Generate a random number from 0-99 */
lr_output_message
("A number between 0 and 99: %d\n", rand() % 100); 15——20 学习啦
页:
[1]
2