Skip to content

Bash: generate file with N random integers

July 3, 2010

Here is an useful bash script that generates a number of random integers into a file. I use it every time I need to test or to measure the execution time of a function or an algorithm over integers (for example sorting algorithms):

#!/bin/bash

#declare the random generation function
fun="od -vAn -N4 -tu4 < /dev/urandom"

#generate a number of integers
for i in {1..100}; do eval $fun >> temp.txt; done

#delete the empty lines
sed '/^$/d' temp.txt > temp1.txt

#delete the leading and trailing spaces
sed 's/^[ \t]*//;s/[ \t]*$//' temp1.txt > numbers.txt

#remove the temporaty files
rm -rf temp.txt temp1.txt
Advertisements
2 Comments
  1. It’s nearly impossible to find knowledgeable people in this particular subject, however, you sound like you know what you’re talking about!

    Thanks

Trackbacks & Pingbacks

  1. QuickSort: Haskell vs C « Carlos Vilhena’s blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: