当前位置:首页 » 操作系统 » linux随机数

linux随机数

发布时间: 2024-07-05 22:10:52

1. linux下的random()和srand(arg), rand()这两个生成的随机数有什么区别。

首先我把这三个函数原型给你看一下
long random(void);
int rand(void);
void srand(unsigned seed);
random返回的是一个0到(2^31 - 1)的long类型整数
rand返回的是一个0到RAND_MAX的int类型整数

而你这里产生的随机数序列是一样的,这个很好解释,因为你知道srand,但是你却不知道还有一个srandom,这个函数是为random设置种子的,参数和srand一样。
我的帮助手册上甚至是这么写的:
The random() and srandom() functions have (almost) the same calling sequence
and initialization properties as the rand(3) and srand(3) functions. The
difference is that rand(3) proces a much less random sequence -- in fact,
the low dozen bits generated by rand go through a cyclic pattern. All of
the bits generated by random() are usable. For example, `random()&01' will
proce a random binary value.

=============================
希望我的回答能给你带来帮助

2. linux中,C语言for语句中的随机数种子。

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int i,j;
srand(time(0));
for( i = 0;i < 1000;i++)
for(j = 0;i < 100;i++){
printf("%3d",rand()%100+1);
printf("\n");
}
return 0;
}

3. 如何在linux中用命令产生一个范围内的随机数

在shell中有一个环境变量RANDOM,它的范围是0--32767
如果我们想要产生0-25范围内的数,如下:
$RANDOM%26
用这个环境变量对26取模,就可以得到最小是0,最大是25的数了。
如果想得到1--68范围内的数,可以这样
$RANDOM%68+1,
前面可以得到最小为0,最大为67的随机数,再加上1,很自然的就可以得到最大为1,最小为68的数了。
如果想得到6--87范围内的数。可以这样
$RANDOM%82+6,
前面可以得到最小为0,最大为81的随机数,再加上6,很自然的就可以得到最大为87,最小为6的数了。

热点内容
随机启动脚本 发布:2025-07-05 16:10:30 浏览:516
微博数据库设计 发布:2025-07-05 15:30:55 浏览:19
linux485 发布:2025-07-05 14:38:28 浏览:299
php用的软件 发布:2025-07-05 14:06:22 浏览:751
没有权限访问计算机 发布:2025-07-05 13:29:11 浏览:425
javaweb开发教程视频教程 发布:2025-07-05 13:24:41 浏览:687
康师傅控流脚本破解 发布:2025-07-05 13:17:27 浏览:234
java的开发流程 发布:2025-07-05 12:45:11 浏览:680
怎么看内存卡配置 发布:2025-07-05 12:29:19 浏览:277
访问学者英文个人简历 发布:2025-07-05 12:29:17 浏览:828