The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 08:24 PM
FTP random files whegra Shell Programming and Scripting 3 11-21-2007 05:22 PM
Random keyvan Shell Programming and Scripting 8 05-24-2007 12:11 PM
Getting a random file davidY Shell Programming and Scripting 6 01-01-2007 05:03 AM
random in ksh pascalbout AIX 1 01-04-2006 10:53 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-01-2008
relle relle is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 6
$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?
  #2 (permalink)  
Old 04-01-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
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.
  #3 (permalink)  
Old 04-01-2008
relle relle is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 6
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
  #4 (permalink)  
Old 07-10-2008
milhan milhan is offline
Registered User
  
 

Join Date: Oct 2002
Location: /home
Posts: 133
$random

hello,

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

thanks
  #5 (permalink)  
Old 07-10-2008
g.pi g.pi is offline
Registered User
  
 

Join Date: Mar 2008
Location: US
Posts: 9
$RANDOM is a built-in bash function that returns a random integer in the range 0 - 32767.
  #6 (permalink)  
Old 07-10-2008
milhan milhan is offline
Registered User
  
 

Join Date: Oct 2002
Location: /home
Posts: 133
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.
  #7 (permalink)  
Old 07-11-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0