The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Random command melaz Shell Programming and Scripting 1 07-10-2008 05:24 PM
FTP random files whegra Shell Programming and Scripting 3 11-21-2007 01:22 PM
Random keyvan Shell Programming and Scripting 8 05-24-2007 09:11 AM
Getting a random file davidY Shell Programming and Scripting 6 01-01-2007 01:03 AM
random in ksh pascalbout AIX 1 01-04-2006 06:53 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-01-2008
Registered User
 

Join Date: Mar 2008
Posts: 6
Stumble this Post!
$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?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-01-2008
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
Stumble this Post!
You want to get lines from a file without reading the file

A possible solution to read lines randomly from a file :
Code:
$ cat words.sh
#!/usr/bin/bash

words_file=words.txt
words_count=$(wc -l $words_file | awk '{print $1}')

for ((i=1; i<=10; i+=1))
do
   word_number=$(($RANDOM % words_count + 1))
   word=$(awk 'NR=='$word_number words.txt)
   echo "$word_number => $word"
done

$ cat words.txt
help
go
three
house
film
datafile
man
save
words
red
blue
$ words.sh
1 => help
7 => man
6 => datafile
1 => help
9 => words
10 => red
4 => house
11 => blue
3 => three
7 => man
$
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 04-01-2008
Registered User
 

Join Date: Mar 2008
Posts: 6
Stumble this Post!
Hi Aigles

My problem is my program has to prompt a user to do a guess game. The user has to guess a letter from a word that the program randomly chooses and then set a number of lives.

If the user guesses a letter correctly, the letter is displayed within the correct position in the word.

i.e
pls enter your guess letter: -----

if the word is hello for example, the user enters e
then the output has to look like this

pls enter your guess letter:-e---

so each dash has to be replaced by the letter and if any guessed letter is wrong, a message should be displayed saying
wrong letter: you have 4 lives left
Reply With Quote
  #4 (permalink)  
Old 07-10-2008
milhan's Avatar
Registered User
 

Join Date: Oct 2002
Location: /home
Posts: 121
Stumble this Post!
$random

hello,

can someone tell me more about $RANDOM? where is it defined and how is it implemented?

thanks
Reply With Quote
  #5 (permalink)  
Old 07-10-2008
Registered User
 

Join Date: Mar 2008
Location: US
Posts: 1
Stumble this Post!
$RANDOM is a built-in bash function that returns a random integer in the range 0 - 32767.
Reply With Quote
  #6 (permalink)  
Old 07-10-2008
milhan's Avatar
Registered User
 

Join Date: Oct 2002
Location: /home
Posts: 121
Stumble this Post!
Quote:
Originally Posted by g.pi View Post
$RANDOM is a built-in bash function that returns a random integer in the range 0 - 32767.
$RANDOM is not a function but a shell variable only found in ksh bash and zsh.
Reply With Quote
  #7 (permalink)  
Old 07-11-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/florida
Posts: 951
Stumble this Post!
In ksh93 the range for the RANDOM variable is 0 - 2**15 and is generated by means of the rand(3) pseudo-random number generator. As an extra precaution, ksh93 checks to see that it never picks the same number twice in a row.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0