6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bash > 乱数を得る > シェル変数RANDOMを使う

Last updated at Posted at 2015-02-28

動作確認

CentOS 6.5

bashで乱数を得たい。
シェル変数RANDOMを使うと0-32767までの整数が得られる。

int_rand_exec
#!/bin/env bash

for idx in $(seq 1 100)
do
  echo $RANDOM
done

実数の乱数を得たい場合は

real_rand_exec
#!/bin/env bash

MAX_VAL=100.0

for idx in $(seq 1 100)
do
  echo "$RANDOM*$MAX_VAL/32768" | bc -l
done

のようにRANDOMの値を変更すればいい。

Link: こちらが参考になりました。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?