当前位置:首页 » 操作系统 » 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-09-14 00:38:54 浏览:193
单片机c语言视频下载 发布:2025-09-14 00:32:10 浏览:344
怎样改变存储位置 发布:2025-09-14 00:28:25 浏览:901
phpedit 发布:2025-09-14 00:26:50 浏览:229
sqlnotlike多个 发布:2025-09-14 00:20:15 浏览:868
ftp映射到本地 发布:2025-09-14 00:12:52 浏览:284
c语言叹号 发布:2025-09-14 00:00:23 浏览:125
rpg怎么电脑改安卓 发布:2025-09-13 23:51:35 浏览:835
c语言i6 发布:2025-09-13 23:51:23 浏览:740
32种算法 发布:2025-09-13 23:50:04 浏览:879