简体   繁体   English

如何在3行中生成随机数-Linux Shell脚本

[英]How do I generate random numbers in 3 lines - Linux Shell Script

I would like to write a code that can generate 3 rows of 6 random numbers spaced out, which shuffle after a given time (0.5 seconds), and no new rows are created, basically 6 random numbers keep generating in 3 rows. 我想编写一个代码,该代码可以生成3行,每行6个随机数,间隔6个随机数,在给定时间(0.5秒)后会随机播放,并且不会创建新行,基本上6个随机数会在3行中继续生成。 The code I have so far is: 到目前为止,我的代码是:

echo " "

echo " "

echo " "



for i in {1..5};

do

for i in {1..1};

do


echo -ne " $(($RANDOM % 100))   $(($RANDOM % 100))   $(($RANDOM % 100))   $(($RANDOM % 100))   $(($RANDOM % 100))   $(($RANDOM % 100))\r"


done

sleep 0.5

done

However, when I try to add the second and third row to this, it doesn't seem to work the way I want it. 但是,当我尝试将第二行和第三行添加到此行时,它似乎无法按照我想要的方式工作。 A sample output could look like: 输出示例如下:

45 88 85 90 44 22 45 88 85 90 44 22

90 56 34 55 32 45 90 56 34 55 32 45

58 99 42 10 48 98 58 99 42 10 48 98

and between these numbers, new ones will generate, keeping only 6 columns and 3 rows. 在这些数字之间,将生成新的数字,仅保留6列和3行。 I have tried making matrix too but it didn't work for me. 我也尝试过制作矩阵,但是对我来说不起作用。

I would recommend you to avoid using the shell for this. 我建议您避免为此使用外壳程序。 The shell is great for automating system administration tasks - eg interact with files, directories, shell commands -, but it is also great to keep people away from learning a 'real' and most powerful programming language. Shell非常适合于自动执行系统管理任务-例如与文件,目录,Shell命令进行交互-但也可以使人们远离学习“真正的”和最强大的编程语言。

python, ruby or perl can help you out. python,ruby或perl可以帮助您。

if all you have is a hammer, everything looks like a nail. 如果您只有锤子,那么一切看起来都像钉子。

eg ruby 例如红宝石

def print_random_numbers(num)
  random_numbers = []
  num.times do |n|
    random_numbers << rand(100)
  end
  puts random_numbers.join(' ')
  puts
end


while true
  3.times do
    print_random_numbers(6)
  end
  sleep 0.5
end

I'm not sure whether I really understand your problem, but: 我不确定我是否真的了解您的问题,但是:

This gets you 6 numbers taken at random from the range 1-100 这使您从1-100的范围内随机抽取6个数字

numbers=$(shuf -i 1-100 -n 6)
echo $numbers

The numbers are selected without repetition. 选择的数字没有重复。 If you want repetition, use -r. 如果要重复,请使用-r。

This gives you a permutation of the numbers drawn before: 这使您可以对之前绘制的数字进行排列:

 echo $numbers | tr ' ' '\n' | shuf | xargs echo

I don't know if you have it finish, but continuing on from the comment, I would fill an indexed array with random values between 1-100 , eg 我不知道您是否已完成,但是从注释继续,我将使用1-100之间的随机值填充索引数组,例如

#!/bin/bash

for ((i = 0; i < 18; i++)); do      ## fill array with random values
    a[i]=$(($RANDOM % 100 + 1))
done

What you would then want is a function you could call, passing the number of values in each row (so you can output a '\\n' after those digits print) and then the array values as arguments to the function to read into a local array within the function (of course, you can just use the original array without worrying about passing elements as arguments, but I prefer using local values within function to preserve values in other scopes unchanged. For that your print function could be something like: 然后,您想要的是一个可以调用的函数,在每行中传递值的数量(以便您可以在打印完这些数字后输出'\\n' ),然后将数组值作为该函数的参数读入本地函数内的数组(当然,您可以使用原始数组而不必担心将元素作为参数传递,但是我更喜欢在函数内使用local值来保持其他范围内的值不变。为此,您的打印函数可能类似于:

## function to print array in shuffled order, using tput for cursor control
prnarray() {
    local n=$1
    local al=( ${@:2} )
    local c=0
    for i in $(shuf -i 0-$((${#al[@]} - 1))); do
        [ "$c" -gt '0' -a $((c % n)) -eq '0' ] && printf "\n"
        printf " %3d" "${al[i]}"
        ((c++))
    done
    printf "\n"
    tput cuu 6   ## tput is used for cursor control to move back to top
}

Then you really don't need much else bu a loop to print the array, sleep for some period of time and then call prnarray again to overlay the output with a new shuffle. 然后,您实际上不需要太多其他步骤就可以循环打印阵列, sleep一段时间,然后再次调用prnarray来用新的shuffle覆盖输出。 eg 例如

tput sc     ## save cursor position

## call prnarray 3 times with 5 sec. delay between displays
declare -i c=0
while (( c < 3 )); do
    prnarray 3 ${a[@]}
    ((c++))
    sleep 5
done

tput rc     ## restore cursor position

Example Use/Output 使用/输出示例

The array will print in the same spot every 5 seconds with the same elements shuffled to different positions within the array. 阵列将每5秒钟在相同的位置打印一次,其中相同的元素将随机排列到阵列中的不同位置。

$ sh randomshuf.sh
  33  30  34
  86  98  48
  94  89  80
  50  57  34
  11  45  57
  80  42  22

Give it a shot and let me know if you have any questions. 试一试,如果您有任何疑问,请告诉我。

Note: to make it 3x6 change the lines: 注意:使其3x6更改行:

 tput cuu 3

and

prnarray 6 ${a[@]}

With those changed your output would resemble: 更改这些内容后,您的输出将类似于:

$ sh randomshuf.sh
  85   9  45  14  18  16
   6  59  43  19  29  58
   7  89  18  72  29  29

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

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