Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > microsoft.public.scripting.vbscript > #11506
| Newsgroups | microsoft.public.scripting.vbscript |
|---|---|
| Subject | Re: How to Randomize characters in a string |
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
| References | <F73BB2A2-98D0-4BBC-B3E5-2C347A597962@microsoft.com> <E80766BD-B20E-4F81-9D51-8CDB55269928@microsoft.com> <dbbba9ca-785e-453a-a665-e6f0ef99d0e1@googlegroups.com> |
| Date | 2017-01-03 17:48 +0100 |
| Message-ID | <XnsA6F2B522EEB5eejj99@194.109.6.166> (permalink) |
doug.reiserrelief@gmail.com wrote on 03 Jan 2017 in
microsoft.public.scripting.vbscript:
> On Tuesday, August 19, 2008 at 7:42:01 PM UTC-5, Old Pedant wrote:
>
>> First of all, if you don't *HAVE* to put the characters into a single
>> string, don't. Put them into an array. But if they are already in an
>> array, convert the string to an array. Example:
>>
>> Randomize ' only do this once per file!
>>
>> Function ShuffleString( str )
>> Dim temp, i, r, swap
>> ReDim temp( Len(str) - 1 )
>> For i = 0 To UBound(temp)
>> temp(i) = Mid(str,i+1,1)
>> Next
>>
>> For i = 0 To UBound(temp)
>> r = INT( RND() * Len(str) )
>> swap = temp(r)
>> temp(r) = temp(i)
>> temp(i) = swap
>> Next
>>
>> ShuffleString = Join(temp,"")
>> End Function
>>
>> foo = ShuffleString( "ALLIGATOR" )
>>
> Thank you Old Pedant!
> Your code works great for my strings
No need for arrays:
function ShuffleString(text)
randomize()
ln = len(text)
for i=1 to ln
r = int( rnd() * ln ) + 1
text = swapLr(text,i,r)
next
ShuffleString = text
end function
function swapLr(tx,i,r)
tempi = mid(tx,i,1)
tempr = mid(tx,r,1)
tx = replLr(tx,r,tempi)
tx = replLr(tx,i,tempr)
swapLr = tx
end function
function replLr(tx,beg,letter)
Set myRegExp = New RegExp
myRegExp.Pattern = "^(.{" & beg-1 & "})."
replLr = myRegExp.replace(tx,"$1"&letter)
end function
response.write ShuffleString( "ALLIGATOR" )
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Back to microsoft.public.scripting.vbscript | Previous | Next — Previous in thread | Next in thread | Find similar
Re: How to Randomize characters in a string doug.reiserrelief@gmail.com - 2017-01-03 04:46 -0800
Re: How to Randomize characters in a string "Mayayana" <mayayana@invalid.nospam> - 2017-01-03 09:01 -0500
Re: How to Randomize characters in a string "Auric__" <not.my.real@email.address> - 2017-01-03 17:54 +0000
Re: How to Randomize characters in a string "Mayayana" <mayayana@invalid.nospam> - 2017-01-03 19:38 -0500
Re: How to Randomize characters in a string "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-01-03 17:48 +0100
Re: How to Randomize characters in a string Dr J R Stockton <reply1700@merlyn.demon.co.uk.invalid> - 2017-01-05 19:23 +0000
Re: How to Randomize characters in a string "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-01-06 00:51 +0100
csiph-web