$random


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting $random
# 1  
Old 04-01-2008
$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  
Old 04-01-2008
You want to get lines from a file without reading the file Smilie

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  
Old 04-01-2008
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  
Old 07-10-2008
$random

hello,

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

thanks
# 5  
Old 07-10-2008
$RANDOM is a built-in bash function that returns a random integer in the range 0 - 32767.
# 6  
Old 07-10-2008
Quote:
Originally Posted by g.pi
$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  
Old 07-11-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Random numbers

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! Write a shell script that will take the sum of two random number? Ex: Random n1 +Random n2 = result i tries to write it but i had some dufficulties ... (3 Replies)
Discussion started by: renegade755
3 Replies

2. 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

3. Shell Programming and Scripting

Random Variable

Hi, Could you please let me know what the following code does, I know that it means generating random numbers, however not sure what is the entire purpose. R=$(($RANDOM % 2)) delay=$(($RANDOM % 10)) if then TEXT='X' else ... (3 Replies)
Discussion started by: susankoperna1
3 Replies

4. Shell Programming and Scripting

Random ordering

1 2 4 5 3 I would like to use a script so that i can randomly rearrange these numbers such as 3 5 2 4 1 Thanks! (3 Replies)
Discussion started by: johnkim0806
3 Replies

5. UNIX for Dummies Questions & Answers

Random Crashing

Over the last month or so my CentOS server has been crashing for reasons I do not know. It has been running for over a year with regular yum updates without problems. The load on the server is perfectly normal with CPU usage at 5-6% and RAM usage at less than half of 32GB of RAM (multiple smaller... (3 Replies)
Discussion started by: spinner0205
3 Replies

6. Programming

random number

How can I choose randomly the row numbers of my file in awk? (4 Replies)
Discussion started by: Homa
4 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. UNIX for Dummies Questions & Answers

random words

Hi there folks, for an exercise for my pupils (you know i am always thinking of them!) i need to randomly re-arrange the words (blank space separated) in a sentence (a line in a textfile). Any inspiration?? Txk so much. (9 Replies)
Discussion started by: eldeingles
9 Replies

9. 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

10. Shell Programming and Scripting

Getting a random file

Hello, I am very new to shell scripting. This problem seems quite easy so it should be quite easy (I hope ^^) I want to get a random file from a directory. this file will be in one subdirectory, and it will contain spaces. code I have got so far: N=find ./*/*.jpg | wc -l ((N=RANDOM%N)) ... (6 Replies)
Discussion started by: davidY
6 Replies
Login or Register to Ask a Question