Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Looking for a SCRAMBLE() Function
Message
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00565279
Message ID:
00565284
Views:
17
This message has been marked as the solution to the initial question of the thread.
Tere're many different ways to build such function. Here's one I just wrote. It works but I didn't do thorough testing.
FUNCTION Scramble (tcStr)
LOCAL lnI, lcRet, lnPos, lnLen

lnLen = Len(tcStr)
* Fill the result string with characters that cannot appear in the source string
lcRet = REPLICATE(Chr(255), lnLen)
=Rand(-1)
* Take characters one by one from the source string 
*	and place randomly into the result string
FOR lnI =1 TO lnLen
  DO WHILE .T.
        lnPos = RandInt(1, lnLen)
	* If randomly selected position is filled already, try again
	IF Substr( lcRet, lnPos ) = Chr(255)
  	    lcRet = Stuff( lcRet, lnPos, 1, Substr(tcStr, lnI,1) )
	    EXIT
	ENDIF
  ENDDO
ENDFOR
RETURN lcRet

FUNCTION RandInt(tnLower, tnUpper)
RETURN INT((tnUpper - tnLower + 1) * RAND( ) + tnLower)
>I was hoping to find a pre-existing function in VFP that would have functionality along the lines of:
>
>SCRAMBLE("word") which would return any of the following:
>
>ordw rodw rowd drow ordw owdr
>
>and so on. Basically, I want to randomly scramble the letters within a word.
>
>Any suggestions?
>
>TIA
--sb--
Previous
Reply
Map
View

Click here to load this message in the networking platform