当前位置:首页 » 操作系统 » 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的数了。

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:607
制作脚本网站 发布:2025-10-20 08:17:34 浏览:899
python中的init方法 发布:2025-10-20 08:17:33 浏览:593
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:777
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:695
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:1023
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:266
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:126
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:815
python股票数据获取 发布:2025-10-20 07:39:44 浏览:726