linux kernel 随机数

linux kernel 生成随机数

Linux kernel 生成随机数

仅以此做一个学习记录。

随机数

随机数kernel 里面有提供一个专门的file:

drivers/char/random.c

API:

/*
 * This function is the exported kernel interface.  It returns some
 * number of good random numbers, suitable for key generation, seeding
 * TCP sequence numbers, etc.  It does not rely on the hardware random
 * number generator.  For random bytes direct from the hardware RNG
 * (when available), use get_random_bytes_arch(). In order to ensure
 * that the randomness provided by this function is okay, the function
 * wait_for_random_bytes() should be called and return 0 at least once
 * at any point prior.
 */
static void _get_random_bytes(void *buf, int nbytes)
{
	__u8 tmp[CHACHA_BLOCK_SIZE] __aligned(4);

	trace_get_random_bytes(nbytes, _RET_IP_);

	while (nbytes >= CHACHA_BLOCK_SIZE) {
		extract_crng(buf);
		buf += CHACHA_BLOCK_SIZE;
		nbytes -= CHACHA_BLOCK_SIZE;
	}

	if (nbytes > 0) {
		extract_crng(tmp);
		memcpy(buf, tmp, nbytes);
		crng_backtrack_protect(tmp, nbytes);
	} else
		crng_backtrack_protect(tmp, CHACHA_BLOCK_SIZE);
	memzero_explicit(tmp, sizeof(tmp));
}

void get_random_bytes(void *buf, int nbytes)
{
	static void *previous;

	warn_unseeded_randomness(&previous);
	_get_random_bytes(buf, nbytes);
}
EXPORT_SYMBOL(get_random_bytes);

/*
 * Wait for the urandom pool to be seeded and thus guaranteed to supply
 * cryptographically secure random numbers. This applies to: the /dev/urandom
 * device, the get_random_bytes function, and the get_random_{u32,u64,int,long}
 * family of functions. Using any of these functions without first calling
 * this function forfeits the guarantee of security.
 *
 * Returns: 0 if the urandom pool has been seeded.
 *          -ERESTARTSYS if the function was interrupted by a signal.
 */
int wait_for_random_bytes(void)
{
	if (likely(crng_ready()))
		return 0;
	return wait_event_interruptible(crng_init_wait, crng_ready());
}
EXPORT_SYMBOL(wait_for_random_bytes);

https://elixir.bootlin.com/linux/latest/source/drivers/char/random.c#L1646

### 如何在 Linux Shell 中生成随机数Linux Shell 环境下,有多种方式可以实现随机数的生成。以下是常见的几种方法及其具体实现: #### 方法一:通过 `/proc/sys/kernel/random/uuid` 和 `cksum` 实现 可以通过读取系统的 UUID 并利用其哈希值来生成伪随机数。这种方法适用于需要一定范围内的整数值。 ```bash #!/bin/bash function rand(){ min=$1 max=$(($2 - $min + 1)) num=$(cat /proc/sys/kernel/random/uuid | cksum | awk -F ' ' '{print $1}') echo $(($num % $max + $min)) } rnd=$(rand 100 500) echo $rnd exit 0 ``` 上述代码定义了一个函数 `rand()` 来生成指定范围内 `[min, max]` 的随机数[^1]。 --- #### 方法二:使用 `awk` 结合内置的 `srand()` 函数 `awk` 提供了一种简单的方式来生成基于种子的随机数。此方法适合于生成标准化的浮点型随机数。 ```bash random_num=$(awk "BEGIN{srand(); print int(rand()*100)}") echo $random_num ``` 这里的关键在于调用了 `srand()` 初始化随机数种子,并通过 `rand()` 获取一个介于 `[0,1)` 范围内的随机数[^2]。乘以所需的最大值即可扩展到更大的区间。 --- #### 方法三:借助 `shuf` 工具生成随机整数 对于更简单的场景,可以直接使用 GNU Coreutils 提供的工具——`shuf`。它能够快速生成一系列不重复或者可重复的随机数列。 单次生成固定区间的随机数: ```bash shuf -i 0-100 -n1 ``` 如果希望生成多个随机数,则调整 `-n` 参数的数量参数。例如生成三个不同的随机数: ```bash shuf -i 0-100 -n3 ``` 此外还可以用于生成带有步长控制的小数形式随机数: ```bash seq 0 .01 1 | shuf | head -n1 ``` 这会创建从 `0` 到 `1` 步长为 `.01` 的序列并从中选取一项作为最终结果[^3]。 --- #### 总结 以上介绍了三种主流技术手段分别对应不同需求层次下的解决方案。每一种都有各自特点以及适用场合,在实际应用过程中可以根据具体情况灵活选用最合适的方案完成任务目标。 ```python import random def generate_random(min_val=0,max_val=99): return random.randint(min_val,max_val) for _ in range(5): print(generate_random()) ``` 尽管这是 Python 版本的例子,但它展示了另一种编程语言中的随机数生成功能对比效果。 相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值