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

热点内容
sdf数据库密码 发布:2025-03-20 20:03:55 浏览:117
戴尔服务器如何开机 发布:2025-03-20 19:58:16 浏览:746
威联通局域网共享ftp 发布:2025-03-20 19:57:39 浏览:113
如何修改服务器编码格式 发布:2025-03-20 19:56:40 浏览:266
java图形界面布局 发布:2025-03-20 19:56:39 浏览:105
java的加载与编译 发布:2025-03-20 19:47:55 浏览:228
c语言回车退出 发布:2025-03-20 19:34:42 浏览:968
好屋源码 发布:2025-03-20 19:24:02 浏览:76
安卓手机怎么改手势接听 发布:2025-03-20 19:23:47 浏览:993
手机wifi在哪个文件夹 发布:2025-03-20 19:22:57 浏览:403