Random numbers with Ruby

ruby -e "puts (1..20).map {rand(10 ** 10).to_s.rjust(10,'0')}"
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
Sample Output
-bash2-2.05b$ uname -sr
FreeBSD 5.4-RELEASE-p14
-bash2-2.05b$ ruby -e "puts (1..20).map {rand(10 ** 10).to_s.rjust(10,'0')}"
6366075576
2917975774
7329745840
8691581041
5890408189
1638634981
0295194105
6344513644
4301587535
4936779610
1481540380
5683771498
6753375812
4915063505
1500897963
8012594603
1481070124
3665078190
0436854511
6307883169

3
By: sil
2009-05-27 19:52:53

These Might Interest You

  • I like the fact the Patola's version uses only ones and zeros, but I also like the sparse output of the other versions. This one combines both of those features and eliminates some unnecessary cruft. You can vary the sparseness by changing "$(($RANDOM % 5))" to another number. The number in this term "$(($RANDOM % 4))" controls how frequently the numbers are output bold.


    21
    echo -e "\e[32m"; while :; do for i in {1..16}; do r="$(($RANDOM % 2))"; if [[ $(($RANDOM % 5)) == 1 ]]; then if [[ $(($RANDOM % 4)) == 1 ]]; then v+="\e[1m $r "; else v+="\e[2m $r "; fi; else v+=" "; fi; done; echo -e "$v"; v=""; done
    dennisw · 2009-09-27 15:30:38 2
  • Use Ruby's standard Curses module to display a Lissajous curve in the console. Replace the "0.2" with different numbers for different curves. Show Sample Output


    2
    ruby -rcurses -e"include Curses;i=0;loop{setpos 12*(Math.sin(i)+1),40*(Math.cos(i*0.2)+1);addstr'.';i+=0.01;refresh}"
    jaymcgavren · 2010-03-09 06:10:47 1
  • 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. Show Sample Output


    3
    seq 6 | awk '{for(x=1; x<=5; x++) {printf ("%f ", rand())}; printf ("\n")}'
    kaan · 2009-03-24 21:33:38 0
  • 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

What Others Think

Even faster echo $RANDOM
rhythmx · 473 weeks and 3 days ago
Apparently you need to learn to read. This generates a random 10 digit character not a random number
sil · 473 weeks and 3 days ago
cat /dev/urandom | hexdump -d | awk '{ print $2$3}' | head -n20 the ruby is still faster ... cheers for choosing ruby
copremesis · 473 weeks and 2 days ago

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: