当前位置:首页 » 操作系统 » 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复制类 发布:2024-07-08 22:46:16 浏览:585
bat脚本set 发布:2024-07-08 22:37:12 浏览:281
数据库来源期刊 发布:2024-07-08 22:36:21 浏览:502
sqldeveloper怎么建立连接配置 发布:2024-07-08 22:27:47 浏览:327
c语言字符T 发布:2024-07-08 22:03:42 浏览:950
曙光服务器bmc口ip 发布:2024-07-08 21:32:05 浏览:202
什么安卓模拟器能多开 发布:2024-07-08 21:04:49 浏览:838
队友脚本 发布:2024-07-08 21:00:37 浏览:293
3b编程图案 发布:2024-07-08 20:47:08 浏览:98
取钱显示密码错误超次数什么原因 发布:2024-07-08 20:30:09 浏览:70