# 生成随机数和随机字符串

## 随机数字

`bash`提供了一个特殊的`$RANDOM`变量（在`ksh`中也支持），是随机选择`0`到`32767`之间的一个整数。简单的方法就是

```bash
echo $RANDOM
```

如果希望上述生成的数字限制在一个给定范围，可以采用取模方式，例如，以下返回数字是`0~9`之间随机：

```
echo $(($RANDOM % 10))
```

另外一种方法是采用`/dev/random`和`/dev/urandom`设备接口通过内核来随机产生数字：

```bash
od -vAn -N4 -tu4 < /dev/urandom
```

或者

```bash
od -An -N2 -i /dev/random
```

> od - dump files in octal and other formats

## 随机字符串

随机字符串的方法是利用了前面生成随机数字的方法结合`md5sum`工具，将随机数字的`md5`计算出来（也就是随机的字符串了）

```bash
echo $RANDOM | md5sum
```

甚至可以再随机一些

```bash
echo $RANDOM | md5sum | md5sum
```

如果要截取指定长度（举例9位）

```bash
echo $RANDOM | md5sum | cut -c 1-9
```

## 参考

* [Bash Shell Generate Random Numbers](http://www.cyberciti.biz/faq/bash-shell-script-generating-random-numbers/)
* [BASH - Generate a Random String](http://utdream.org/post.cfm/bash-generate-a-random-string)
* [Bash: How to generate a random number within given boundaries](https://makandracards.com/makandra/17289-bash-how-to-generate-a-random-number-within-given-boundaries)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/develop/shell/bash/generate_random_number_and_string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
