randomization


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting randomization
# 1  
Old 05-16-2007
randomization

I have two files:


First file with 10 words, as follow:

randomword1, randomword2, randomword3, randomword4, etc...

Second File shell script.
Code:
word=$(cat hangman_words | cut -d" " -f1)
letters=$(echo $word | wc -c)
letters=$(( $letters - 1 ))
echo $letters
echo $word

The script above will give always give me the first word in the file. I need it to choose a random one each time.

how can i randomly choose a diffrent one everytime i run a script. Hopefully with out using sed or awk.

thx in advance

K

Last edited by keyvan; 05-16-2007 at 09:17 AM.. Reason: Additional Information
# 2  
Old 05-16-2007
You can do something like that :
Code:
random_word() 
{
   if [ -z "$all_words" ]
   then
      sed '1s/,/ /g' random_words_file | read all_words 
      set -A words -- ${all_words}
   fi  
   word_index=$(( $RANDOM % ${#words[@]} ))
   echo ${words[$word_index]}
}

word=$(random_word)

Jean-Pierre.
# 3  
Old 05-16-2007
Quote:
Originally Posted by aigles
You can do something like that :
Code:
random_word() 
{
   if [ -z "$all_words" ]
   then
      sed '1s/,/ /g' random_words_file | read all_words 
      set -A words -- ${all_words}
   fi  
   word_index=$(( $RANDOM % ${#words[@]} ))
   echo ${words[$word_index]}
}

word=$(random_word)

Jean-Pierre.
Jean i cant use sed in any of my scripting.. dont ask me why but thats the harsh truth. how can the above script be changed to remove the sed section?

thx mate

K
# 4  
Old 05-16-2007
No homework questions on the forums.
# 5  
Old 05-16-2007
Quote:
Originally Posted by jim mcnamara
No homework questions on the forums.
jim,

np at all... i am trying to learn shell scripting and the only way for me to learn is to ask questions, and since i have no teachers or instructors to fall back on, then i can only use these forums to learn as much as i can. Please except my sincere apology if this post has offended any one.

K
# 6  
Old 05-16-2007
you can use tr instead of sed :
Code:
      tr ',' ' ' < random_words_file | read all_words

Jean-Pierre.
# 7  
Old 05-16-2007
Quote:
Originally Posted by aigles
you can use tr instead of sed :
Code:
      tr ',' ' ' < random_words_file | read all_words

Jean-Pierre.
gives this error

hang: line 36: set: -A: invalid option
set: usage: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]


K
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Randomization a matrix - perl / Shell

Hello all, I have a tricky question! (at least for me it is!). I'll try to explain it carefully here. Hope you can help me solving the whole or even parts of it! Here it is: I have a big input 0\1 table as a very simplified one is shown below: (The last row and column are the sum and... (0 Replies)
Discussion started by: @man
0 Replies
Login or Register to Ask a Question