简体   繁体   English

使用 Bash 在给定的特定数字范围内生成随机浮点数

[英]Generate random float number in given specific range of numbers using Bash

can someone help me in generating random float number with given specific range of number in bash (shell).有人可以帮助我在bash(shell)中生成具有给定特定数字范围的随机浮点数。 I found below command but is their any other option ?我找到了下面的命令,但他们还有其他选择吗? (using awk or $rand commands.) (使用 awk 或 $rand 命令。)

jot -p2  0 1

If you have GNU coreutils available, you could go with:如果您有可用的 GNU coreutils,则可以使用:

seq 0 .01 1 | shuf | head -n1

Where 0 is the start (inclusive), .01 is the increment, and 1 is the end (inclusive).其中0是开始(包括), .01是增量, 1是结束(包括)。

Without any external utilities, generate random real between 3 and 16.999:在没有任何外部实用程序的情况下,生成 3 到 16.999 之间的随机实数:

a=3
b=17
echo "$((a+RANDOM%(b-a))).$((RANDOM%999))"

To generate 1 random floating point number between 3 and 17:要生成 3 到 17 之间的 1 个随机浮点数:

$ awk -v min=3 -v max=17 'BEGIN{srand(); print min+rand()*(max-min+1)}'
16.4038

To generate 5 random floating point numbers between 3 and 17:要生成 3 到 17 之间的 5 个随机浮点数:

$ awk -v min=3 -v max=17 -v num=5 'BEGIN{srand(); for (i=1;i<=num;i++) print min+rand()*(max-min+1)}'
15.1067
4.79238
3.04884
11.3647
15.1132

Massage to suit.按摩适合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM