LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2012, 10:01 AM   #1
dtmc
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Rep: Reputation: Disabled
Bash - Random word generator


I've written a small script to generate random words, which seems to work reasonably well except for the fact that it's generating far too many 'a's (that's the single letter 'a', not words starting with 'a'. It's about 1 'a' in every 6 words generated). Could someone have a look and suggest where I may be going wrong?

For information, if it's relevant, I'm using Bash 4.2.20 on Arch.

Many thanks,

David Shaw



#!/bin/bash

if [ ! -e 'wordlist' ]; then
aspell dump master | sort | uniq > wordlist
fi

LINES=`cat 'wordlist' | wc -l`

for WORD in 1 2 3 4; do
RAND=`od -N4 -t u4 /dev/urandom | head -1 | cut -d' ' -f2`
LINE=$(( RAND % LINES + 1 ))
head -$LINE 'wordlist' | tail -1
done
 
Old 03-14-2012, 10:35 AM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
The problem is coming when RAND is set empty, and so LINE gets the value 1. ( (0 % anything) + 1 is always 1. ) The value of the wordlist at line 1 is 'a'.

Code:
+ RAND=
+ echo 'RAND is '
RAND is
You should scrutinize this line -- probably break it up a bit -- and determine under what conditions that behaviour is ocurring.

Code:
#Needs fixing
RAND=`od -N4 -t u4 /dev/urandom | head -1 | cut -d' ' -f2`

Last edited by jhwilliams; 03-14-2012 at 10:37 AM.
 
Old 03-14-2012, 12:10 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


bash has a built-in RANDOM environment variable, BTW, so unless you need industrial-strength randomness you can remove the complex external command.

You can also re-seed RANDOM at any time by simply setting it to any string e.g.

Code:
RANDOM=$( date '+%s' )
Edit: I just remembered this as well:

Code:
shuf -n 4 wordlist
This will output four random lines from the file.

Edit2: And if you want to stick with the script, use this to print a line number instead of head+tail:

Code:
sed -n "${LINE}p" wordlist

Last edited by David the H.; 03-14-2012 at 12:17 PM. Reason: as stated
 
Old 03-14-2012, 02:49 PM   #4
dtmc
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Many thanks for the replies :-)

@jhwilliams
D'oh! Never even considered that. I knew it would be *something* simple

@David the H.
The problem with using the built in is that it only returns 32768 possible values and there's well over 100,000 entries in wordlist but other than that, yeah, it's far easier. shuf looks like the way to go, though and I guess I'll end up using it - but I still want to fix my script as a learning exercise.

Again, to both, many thanks

David Shaw
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A very simple random generator puntino Programming 6 10-12-2011 07:10 AM
Random date generator porkcharsui Linux - Newbie 5 01-26-2010 01:58 AM
I need a random number generator newbiesforever Linux - Software 5 05-22-2009 02:04 PM
random number generator vkmgeek Programming 3 09-02-2008 09:41 AM
Random Sentence Generator? gandil Linux - Newbie 7 01-01-2006 10:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration