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

What Others Think

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