Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
nexela
Wizard


Joined: 15 Jan 2002
Posts: 1644
Location: USA

PostPosted: Sun Sep 26, 2004 12:54 pm   

Looking for ways to make a string display random
 
Ok I have a var

var=bbcde

what would be some ways to change it to a random order.

varRand=cbdeb
Reply with quote
Rorso
Wizard


Joined: 14 Oct 2000
Posts: 1368

PostPosted: Sun Sep 26, 2004 1:16 pm   Re: Looking for ways to make a string display random
 
nexela wrote:
Ok I have a var

var=bbcde

what would be some ways to change it to a random order.

varRand=cbdeb

#VAR randomizer {%if( %1="", "", %if( %random( 1, 2) = 1, %concat( %left( %1, 1), @randomizer(%right(%1,1))), @randomizer(%concat(%right(%1,1),%left(%1,1)))))}

Example:
#echo @randomizer("abc")
-> acb

Edit: That one didn't work very well after all :P. This is a better method:
#ALIAS randomizer2 {
; %1 = string
temp = "%1"
new = ""
#while (@temp != "") {
index = %random( 1, %len( @temp))
new = %concat( @new, %copy( @temp, @index, 1))
temp = %delete( @temp, @index, 1)
}
#echo @new
}
Reply with quote
nexela
Wizard


Joined: 15 Jan 2002
Posts: 1644
Location: USA

PostPosted: Sun Sep 26, 2004 3:52 pm   
 
Ok after much offline tinkering around this is what I came up with

#LOOP %len( @cchatstr.mid) {
cchatstr.wordpos=%random( 1, %len( @cchatstr.mid))
cchatstr.jumbword=%concat( @cchatstr.jumbword, %copy( @cchatstr.mid, @cchatstr.wordpos, 1))
cchatstr.mid=%delete( @cchatstr.mid, @cchatstr.wordpos, 1)
}

Which is basicly the same thing but I think a #While (as Rorso used) might be better :P if anyone is interested this is the full script basicly it just jumbles the middle of any 4 or more letter word, I am gonna end up tidying it up later on but for now


#AL JumbleWords {
cchatstr=""
#LOOP %numwords( "%-2") {
#IF (%len( %word( "%-2", %i))>=4) {
cchatstr.word=""
cchatstr.mid=""
cchatstr.jumbword=""
cchatstr.word=%word( "%-2", %i)
cchatstr.begin=%copy( @cchatstr.word, 1, 1)
cchatstr.end=%copy( @cchatstr.word, %len( @cchatstr.word), 1)
cchatstr.mid=%copy( @cchatstr.word, 2, %eval( %len( @cchatstr.word)-2))
#LOOP %len( @cchatstr.mid) {
cchatstr.wordpos=%random( 1, %len( @cchatstr.mid))
cchatstr.jumbword=%concat( @cchatstr.jumbword, %copy( @cchatstr.mid, @cchatstr.wordpos, 1))
cchatstr.mid=%delete( @cchatstr.mid, @cchatstr.wordpos, 1)
#NOOP
}
cchatstr.sent=%concat( @cchatstr.sent, %concat( @{cchatstr.begin}, @{cchatstr.jumbword}, @{cchatstr.end})%char( 32))
} {cchatstr.sent=%concat( @cchatstr.sent, %word( "%-2", %i)%char( 32))}
}
%1 @cchatstr.sent
}

Usage: jumblewords chan string here
Used Pretty Print
Syntax Colourizer
Reply with quote
LightBulb
MASTER


Joined: 28 Nov 2000
Posts: 4817
Location: USA

PostPosted: Mon Sep 27, 2004 2:20 pm   
 
The scrambling could be done with a single variable.

#LOOP 1,%len( @scramble) {
#VAR scramble {%left( @scramble, %i - 1)%copy( @scramble, %random( %i, %len(@scramble)), 1)%right( @scramble, %i - 1)}
#VAR scramble {%left( @scramble, %i)%remove( %copy( @scramble, %i, 1), %right( @scramble, %i))}
}


Explanation:
Loop once for each character in the string. Each iteration does two #VARIABLE commands.
The first #VARIABLE command has the scrambled characters on the left, the unused characters on the right, and puts one of those unused characters at random in the middle.
The second #VARIABLE command simply removes the newly chosen character from the unused characters.
_________________
LightBulb
Senior member

Most scripts in this forum are written for Command Line entry.
Don't even open the Settings Editor unless its use is specified or obvious.
Reply with quote
nexela
Wizard


Joined: 15 Jan 2002
Posts: 1644
Location: USA

PostPosted: Mon Sep 27, 2004 8:03 pm   
 
Thanks Lightbulb You have allowed me to accomplish it with fewer Vars :P

Of Course it took me a few minutes of wondering why it would work on its own but not in my script and then I realized *WHAP SELF* that I was already inside one loop..... changed it to %j and now it works like a charm with less variables too :P.
_________________
Zmud Support Library
Zmud Knowledge Base
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net