![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| $random | relle | Shell Programming and Scripting | 6 | 07-11-2008 02:59 AM |
| 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 02:22 PM |
| Getting a random file | davidY | Shell Programming and Scripting | 6 | 01-01-2007 02:03 AM |
| random in ksh | pascalbout | AIX | 1 | 01-04-2006 07:53 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Random
My problem is as follow and i hope you can help:
I currently have this function: Code:
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" ;;
6 ) echo "bugs" ;; 7 ) echo "inaccessible" ;;
8 ) echo "country" ;; 9 ) echo "folder" ;;
10 ) echo "individual" ;; 11 ) echo "youngest" ;;
12 ) echo "disco" ;; 13 ) echo "disturbed" ;;
14 ) echo "company" ;; 15 ) echo "scientific" ;;
16 ) echo "disaster" ;; 17 ) echo "protection" ;;
18 ) echo "curiously" ;; 19 ) echo "deranging" ;;
21 ) echo "facilities"
esac
}
Code:
. hangman_words
word=$(stored_word)
letters=$(echo $word | wc -c)
letters=$(( $letters - 1 ))
template="$(echo $word | tr '[a-z A-Z 0-9]' '.')"
remaining=$letters
Can any one shed a light on this please? K |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Code:
number=$(( RANDOM % 21 + 1 )) word=$( sed -n "$number p" words_list_file ) letters=$(echo $word | wc -c) letters=$(( $letters - 1 )) template="$(echo $word | tr '[a-z A-Z 0-9]' '.')" remaining=$letters |
|
#3
|
|||
|
|||
|
Quote:
You are a star Keyvan Last edited by keyvan; 05-24-2007 at 05:25 AM. Reason: more information |
|
#4
|
||||
|
||||
|
You can store all the words in a file (assume words.txt).
Code:
stored_word()
{
if [ -z "$stored_words_count" ]
then
stored_words_count=$(wc -l < words.txt)
fi
number=$RANDOM
let "number = number % stored_words_count + 1"
tail +$number words.txt | head -1
}
Code:
init_stored_words()
{
while read w
do
words[${#words[@]}]=$w
done < words.txt
stored_words_count=${#words[@]}
}
stored_word()
{
[ -z "$stored_words_count" ] && init_stored_words
number=$RANDOM
let "number %= stored_words_count"
echo ${words[$number]}
}
Last edited by aigles; 05-24-2007 at 09:31 AM. Reason: Modify number calculation for tail/head solution. |
|
#5
|
|||
|
|||
|
Quote:
Code:
no=$(( RANDOM % 21 + 1 )) while read str do (( no = no - 1 )) if [[ $no -eq 0 ]]; then word=$str break fi done < words_list_file letters=$(echo $word | wc -c) letters=$(( $letters - 1 )) template="$(echo $word | tr '[a-z A-Z 0-9]' '.')" remaining=$letters |
|
#6
|
|||
|
|||
|
Quote:
[TEXT] word1 word2 word3 word4 etc.. [/TEXT] K |
|
#7
|
|||
|
|||
|
Quote:
|
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|