Random Line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Random Line
# 1  
Old 11-13-2002
Random Line

I am trying to build a script written in ksh93 that pulls a random line out of each of four text files.

Since the files are all different, I can't think of a way to take the number of lines (in one case, let's say 50), then randomly pick a line out, making sure that all lines have a fair chance...

I tried playing with some variations of division and comparison of the $RANDOM variable, but I was unsuccessful in making sure I didn't pick the same few lines over and over...

Any suggestions?
# 2  
Old 11-13-2002
try something like:

VAR=`expr $RANDOM % 50`
# 3  
Old 11-13-2002
I'll give that a shot!
I tried using division, but not using % (I can't remember what it's called ATM...). It appears that I get a good mix out of it!

Thanks crashnburn - I'll post my script if it turns out how I want it (i.e. - I don't abandon it first Smilie ...)

Last edited by LivinFree; 11-13-2002 at 07:14 PM..
# 4  
Old 11-14-2002
Suppose that you want to pick a random line froma file that has exactly 100 lines. You can label the lines 0 through 99. Then if you get a random integer, you just take the last two digits as your line number.

Those last 2 digits are the remainder that your get when you divide the random integer by 100. So for example, if the random integer is 10726352; you would have a remainder of 52 after dividing 10726352 by 100. This is expressed as
10726352 % 100 = 52
and % is sometimes called "the remainder function". And sometimes "modulus".

But is was important to divide by 100 since we had 100 lines in our file. When we divide by 100, we have 100 possible remainders, so each line in the file has a chance.

To make this general, don't divide by 100 (or 50) all the time, divide by the count of items.
# 5  
Old 11-14-2002
Thanks to Perderabo and Crashburn for this idea...

Code:
nlines=`wc -l filename`
VAR=`expr $RANDOM % $nlines + 1`

we need to add 1 so that we genrate line numbers between 1 to $nlines and not 0 to $nlines -1

or this should also work... here I'm ensuring by dividing with 32768 that the "random" number generated will fall within the no of lines that file contains.

On hp-ux 10, 11 we have man ksh giving

0 <= $RANDOM <= 32767

Code:
nlines=`wc -l filename`
VAR=`expr \( 1 + $RANDOM \) \* $nlines / 32768`

you can see the range for RANDOM by referring "man ksh" for your implementation...

Cheers!
Vishnu.

Last edited by Vishnu; 11-14-2002 at 12:37 PM..
# 6  
Old 11-14-2002
assuming a file size of 5 lines execute both these loops on your machine... you need to type CTRL+C to break the loops when you want...

Quote:
while [ 1 -gt 0 ]; do
expr $RANDOM % 5 + 1
done > newfile
Quote:
while [ 1 -gt 0 ]; do
expr \( 1 + $RANDOM \) \* 5 / 32768
done > newfile2
you will notice that newfile2 for most of the time won't contain 5... so the second way won't lead to a uniform distribution between 1 and $nlines... in fact most of the time it won't generate $nline at least for small $nline values...
# 7  
Old 11-20-2002
Well, I used the basic suggestions above, and came up with this fun little waste of time (in a post below - I couldn't attach the file, even though it's only 12k)

A little background - this is based on the BOfH Excuse Calendar. If you don't know who / what that is, read over here first:
http://bofh.ntk.net/Bastard.html

It's stored in shar format, created by GNU shar.

the "excuse" script assumes you have a working "/bin/ksh", and have common utilities like sed in your PATH.
excuse.web will output a weak excuse for HTML, and is meant to be called from serve_exc. serve_exc assumes that you have netcat (nc), and it's in your PATH, and it was compiled with GAPING_SECURITY_HOLE defined (to allow it to use the "-l" option)... It'll listen on port 8080 for a connection from a web browser, run excuse.web, then start over...

It may take some fiddling, but take the code below, put it in a file called excuse.txt, and type "sh excuse.txt". Then "./excuse" and repeat for endless hours of fun!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

How to insert random numbers into each line?

I have a file contains thousands of lines. I want to insert n random numbers into each line at specific position. Like this: 0 22…… 1 33…… …… …… I want to insert 3 random numbers from position 2 to 4 into each line. 0 325 22…… 1 685 33…… …… …… Please use CODE tags when... (8 Replies)
Discussion started by: hhdzhu
8 Replies

3. UNIX for Dummies Questions & Answers

Changing a special line and Byte in a random file

Hello I created 3 files by: dd if=/dev/urandom bs=1024 count=1000000 of=./testfile1 dd if=/dev/urandom bs=1024 count=5000000 of=./testfile2 dd if=/dev/urandom bs=1024 count=10000000 of=./testfile3 Now I want to know how to make a change in a specific byte and/or line of theses files? (2 Replies)
Discussion started by: frhling
2 Replies

4. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

5. Shell Programming and Scripting

Parse large file on line count (random lines)

I have a file that needs to be parsed into multiple files every time there line contains a number 1. the problem i face is the lines are random and the file size is random. an example is that on line 4, 65, 187, 202 & 209 are number 1's so there has to be file breaks between all those to create 4... (6 Replies)
Discussion started by: darbs121
6 Replies

6. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

7. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

8. Shell Programming and Scripting

Read random line from a text file

I have a text file with hundreds of lines, i wish to run a script and reads a random line to pass it to another command line such as: for line in `cat file |grep random line`; do echo $line |mail my@example.com ; done thank you (6 Replies)
Discussion started by: Bashar
6 Replies

9. Shell Programming and Scripting

$random

I need to use the $RANDOM command to get a line from a list of lines in a file randomly. file is help go three house film how do i randomly get one word without looking into the file? (6 Replies)
Discussion started by: relle
6 Replies

10. Shell Programming and Scripting

Random

My problem is as follow and i hope you can help: I currently have this function: stored_word() { number=$RANDOM let "number %= 21" case $number in 0 ) echo "energy" ;; 1 ) echo "touch" ;; 2 ) echo "climbing" ;; 3 ) echo "declare" ;; 4 ) echo "marry" ;; 5 ) echo "relax" ... (8 Replies)
Discussion started by: keyvan
8 Replies
Login or Register to Ask a Question