Display rows and columns of random numbers with awk

seq 6 | awk '{for(x=1; x<=5; x++) {printf ("%f ", rand())}; printf ("\n")}'
Displays six rows and five columns of random numbers between 0 and 1. If you need only one column, you can dispense with the "for" loop.
Sample Output
0.237788 0.291066 0.845814 0.152208 0.585537 
0.193475 0.810623 0.173531 0.484983 0.151863 
0.366957 0.491736 0.910094 0.265257 0.893188 
0.220351 0.631798 0.571077 0.332158 0.104455 
0.502931 0.567394 0.854165 0.040141 0.108022 
0.639396 0.013111 0.720184 0.101814 0.482945

3
By: kaan
2009-03-24 21:33:38

These Might Interest You

  • There's been a few times I've needed to create random numbers. Although I've done so in PERL, I've found Ruby is actually faster. This script generates 20 random "10" digit number NOT A RANDOM NUMBER. Replace 20 (1..20) with the amount of random numbers you need generated Show Sample Output


    3
    ruby -e "puts (1..20).map {rand(10 ** 10).to_s.rjust(10,'0')}"
    sil · 2009-05-27 19:52:53 3
  • Build an awk array with all commands and then select a random one at the end. This avoids spawning extra processes for counting with wc or generating random numbers. Explicitly call /bin/ls to avoid interactions with aliases.


    -4
    man $(/bin/ls /bin | awk '{ cmd[i++] = $0 } END { srand(); print cmd[int(rand()*length(cmd))]; }')
    emilsit · 2010-08-20 17:31:02 1

  • 2
    p(){ printf "\033[%d;%dH\033[4%dm \033[m" $((RANDOM%LINES+1)) $((RANDOM%COLUMNS+1)) $((RANDOM%8)); }; clear;while :;do p; sleep .001;done
    cfajohnson · 2009-12-17 15:25:28 0
  • Console screensaver.


    9
    alias screensaver='for ((;;)); do echo -ne "\033[$((1+RANDOM%LINES));$((1+RANDOM%COLUMNS))H\033[$((RANDOM%2));3$((RANDOM%8))m$((RANDOM%10))"; sleep 0.1 ; done'
    lkj · 2011-03-24 16:58:14 5

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: