LoginSignup
5

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: こちらが参考になりました。

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
5