niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
randomisation
Post new topic   Reply to topic Page 1 of 1 [8 Posts] View previous topic :: View next topic
Author Message
Fun-Ji Sun
*nix forums beginner


Joined: 10 Feb 2005
Posts: 4

PostPosted: Thu Feb 10, 2005 12:44 am    Post subject: randomisation Reply with quote

Dear comp.unix.shell,

I'm trying to learn techniques of randomisation. Could someone show me how
to write a script that chooses a random line from the input file, and
prints it to the screen with the characters rearranged randomly? (There's
no whitespace within each line of the input file.) Such a script could be
used with a word list as input to practice anagram solving, for example.
Thanks for any help; I'm a beginner.
Back to top
Janis Papanagnou
*nix forums Guru


Joined: 24 Feb 2005
Posts: 450

PostPosted: Thu Feb 10, 2005 12:53 am    Post subject: Re: randomisation Reply with quote

Fun-Ji Sun wrote:
Quote:
Dear comp.unix.shell,

I'm trying to learn techniques of randomisation. Could someone show me how
to write a script that chooses a random line from the input file, and
prints it to the screen with the characters rearranged randomly? (There's
no whitespace within each line of the input file.) Such a script could be
used with a word list as input to practice anagram solving, for example.
Thanks for any help; I'm a beginner.

If you have no specific requirements for the quality of randomness you
can use the builtin RANDOM variable of ksh.

The following code will select a random line from a file "yourfile"...

typeset filename="yourfile"
typeset -i linecount=$( wc -l < "${filename}" )
typeset -i lineno=$(( RANDOM % linecount + 1 ))
typeset line=$( sed -n ${lineno},${lineno}p < "${filename}" )

To shuffle the characters you have to randomly permute them, e.g. ...

typeset -i linelen=${#line}
for (( i = 0; i < linelen-1; i++ ))
do
typeset -i sel=$(( (i+1) + RANDOM % (linelen - i) ))
line=${line:0:i}${line:$((sel)):1}${line:$((i+1)):$((sel-i-1))}
line=${line}${line:$((i)):1}${line:$((sel+1))}
done

print - "${line}"

You need ksh93 to run this code.

Janis
Back to top
Chris F.A. Johnson
*nix forums Guru


Joined: 20 Feb 2005
Posts: 2268

PostPosted: Thu Feb 10, 2005 1:26 am    Post subject: Re: randomisation Reply with quote

On Thu, 10 Feb 2005 at 00:44 GMT, Fun-Ji Sun wrote:
Quote:
Dear comp.unix.shell,

I'm trying to learn techniques of randomisation. Could someone show me how
to write a script that chooses a random line from the input file, and
prints it to the screen with the characters rearranged randomly? (There's
no whitespace within each line of the input file.) Such a script could be
used with a word list as input to practice anagram solving, for example.
Thanks for any help; I'm a beginner.

I'm assuming you are using a shell that sets the $RANDOM
variable. If not, you'll have to generate the random number
another way, possibly with awk.

To extract a line at random, find the number of lines in the file,
then use sed to extract a random line in the correct range:

lines=$(( $(wc -l < FILENAME) ))
line=$(sed -n "$(( $RANDOM % $lines + 1 )){p;q;}")

To rearrange the characters of the line randomly:

printf "%s\n" "$line" | sed 's/./&\
/g' |
awk 'BEGIN { srand() } {printf "%d\t%s\n", rand() * 100000, $0}' |
sort -n | cut -f2 | tr -d '\012'
printf "\n"


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
===================================================================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Back to top
Bill Marcum
*nix forums Guru


Joined: 28 Mar 2005
Posts: 1264

PostPosted: Thu Feb 10, 2005 4:36 pm    Post subject: Re: randomisation Reply with quote

On Thu, 10 Feb 2005 00:44:08 +0000 (UTC), Fun-Ji Sun
<fjs@example.com> wrote:
Quote:
Dear comp.unix.shell,

I'm trying to learn techniques of randomisation. Could someone show me how
to write a script that chooses a random line from the input file, and
prints it to the screen with the characters rearranged randomly? (There's
no whitespace within each line of the input file.) Such a script could be
used with a word list as input to practice anagram solving, for example.
Thanks for any help; I'm a beginner.

Some systems have the command "an" to generate anagrams.
Back to top
Janis Papanagnou
*nix forums Guru


Joined: 24 Feb 2005
Posts: 450

PostPosted: Thu Feb 10, 2005 9:26 pm    Post subject: Re: randomisation Reply with quote

Fun-Ji Sun wrote:
Quote:

Dear Janis - thank you, but I haven't been able to get this code to work.
I copied it into a file which I made executable, replacing "yourfile" with
"Z-words", which is a file containing all the words from my wordlist that
include a Z. I called it janis, and ran it from ksh, getting the
following:

Sorry, to avoid the linebreaks with the posting I ad-hoc added code
which was simply wrong.

Here is the original version that I had tested on my machine...


typeset filename="${1Confused}"
typeset -i linecount=$( wc -l < "${filename}" )
typeset -i lineno=$(( RANDOM % linecount + 1 ))
typeset line=$( sed -n ${lineno},${lineno}p < "${filename}" )

typeset -i linelen=${#line}
for (( i = 0; i < linelen-1; i++ ))
do
typeset -i sel=$(( (i+1) + RANDOM % (linelen - i) ))
line=${line:0:i}${line:$((sel)):1}${line:$((i+1)):$((sel-i-1))}\
${line:$((i)):1}${line:$((sel+1))}
done

print - "${line}"



Janis
Back to top
Fun-Ji Sun
*nix forums beginner


Joined: 10 Feb 2005
Posts: 4

PostPosted: Thu Feb 10, 2005 9:31 pm    Post subject: Re: randomisation Reply with quote

Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
Quote:
Fun-Ji Sun wrote:

I'm trying to learn techniques of randomisation. Could someone show me how
to write a script that chooses a random line from the input file, and
prints it to the screen with the characters rearranged randomly?

If you have no specific requirements for the quality of randomness you
can use the builtin RANDOM variable of ksh.

The following code will select a random line from a file "yourfile"...

typeset filename="yourfile"
typeset -i linecount=$( wc -l < "${filename}" )
typeset -i lineno=$(( RANDOM % linecount + 1 ))
typeset line=$( sed -n ${lineno},${lineno}p < "${filename}" )

To shuffle the characters you have to randomly permute them, e.g. ...

typeset -i linelen=${#line}
for (( i = 0; i < linelen-1; i++ ))
do
typeset -i sel=$(( (i+1) + RANDOM % (linelen - i) ))
line=${line:0:i}${line:$((sel)):1}${line:$((i+1)):$((sel-i-1))}
line=${line}${line:$((i)):1}${line:$((sel+1))}
done

print - "${line}"

You need ksh93 to run this code.

Janis

Dear Janis - thank you, but I haven't been able to get this code to work.
I copied it into a file which I made executable, replacing "yourfile" with
"Z-words", which is a file containing all the words from my wordlist that
include a Z. I called it janis, and ran it from ksh, getting the
following:

$ ./janis
A
$ ./janis
I
$ ./janis
ZZZ
$ ./janis
TSS
$ ./janis
XX
$ ./janis
A
$ ./janis
E
$ ./janis
OOOO
$ ./janis
III
$ ./janis
CCCC
$ ./janis
SM

which is interesting, but not anagrams of the words in my file. Any hints
as to what I might change to get it to work? I am too much a beginner to
figure it out.

Thank you!
Back to top
Fun-Ji Sun
*nix forums beginner


Joined: 10 Feb 2005
Posts: 4

PostPosted: Thu Feb 10, 2005 10:06 pm    Post subject: Re: randomisation Reply with quote

Thanks Chris, but I couldn't get it to work. See below.

Chris F.A. Johnson <cfajohnson@gmail.com> wrote:

Quote:
I'm assuming you are using a shell that sets the $RANDOM
variable. If not, you'll have to generate the random number
another way, possibly with awk.

To extract a line at random, find the number of lines in the file,
then use sed to extract a random line in the correct range:

lines=$(( $(wc -l < FILENAME) ))
line=$(sed -n "$(( $RANDOM % $lines + 1 )){p;q;}")

This second line hangs when I enter it at the ksh prompt.

Quote:
To rearrange the characters of the line randomly:

printf "%s\n" "$line" | sed 's/./&\
/g' |
awk 'BEGIN { srand() } {printf "%d\t%s\n", rand() * 100000, $0}' |
sort -n | cut -f2 | tr -d '\012'
printf "\n"

All this does for me is inserts spaces between characters of the line fed
to it, prepends a random number, sorts numerically (which does nothing),
then chops off the number and the spaces and I'm left with what I started
with. Does it do something different for you?

Thanks for any further help --Fun-Ji Sun
Back to top
Fun-Ji Sun
*nix forums beginner


Joined: 10 Feb 2005
Posts: 4

PostPosted: Thu Feb 10, 2005 10:11 pm    Post subject: Re: randomisation Reply with quote

Fun-Ji Sun <fjs@example.com> wrote:

Quote:
printf "%s\n" "$line" | sed 's/./&\
/g' |
awk 'BEGIN { srand() } {printf "%d\t%s\n", rand() * 100000, $0}' |
sort -n | cut -f2 | tr -d '\012'
printf "\n"

All this does for me is inserts spaces between characters of the line fed
to it,
[snip]


Ah, that would be because I copied it with a space in the first sed
instead of a linefeed...

Sorry!
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [8 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 9:40 pm | All times are GMT
navigation Forum index » Programming » shell
Jump to:  


Free Games | Loans | Tour Management Software | Secured Loans | Credit Report
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2189s ][ Queries: 11 (0.0994s) ][ GZIP on - Debug on ]