Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > microsoft.public.scripting.vbscript > #11494 > unrolled thread

Re: How to Randomize characters in a string

Started bydoug.reiserrelief@gmail.com
First post2017-01-03 04:46 -0800
Last post2017-01-06 00:51 +0100
Articles 7 — 5 participants

Back to article view | Back to microsoft.public.scripting.vbscript

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#11494 — Re: How to Randomize characters in a string

Fromdoug.reiserrelief@gmail.com
Date2017-01-03 04:46 -0800
SubjectRe: How to Randomize characters in a string
Message-ID<dbbba9ca-785e-453a-a665-e6f0ef99d0e1@googlegroups.com>
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

[toc] | [next] | [standalone]


#11496

From"Mayayana" <mayayana@invalid.nospam>
Date2017-01-03 09:01 -0500
Message-ID<o4gao7$rcp$1@dont-email.me>
In reply to#11494
<doug.reiserrelief@gmail.com> wrote

  I don't see the original post. I'm guessing it's
coming out of Google Groups. You might consider
using a real newsreader and a real usenet server,
so that you can use the real usenet newsgroups.

   In any case, Randomize should be called with
every call to Shuffle. Randomize without a parameter
calls Timer to get a seed number for shuffling. You can
also use a unique numeric parameter if you like. Without
calling Randomize you'll get the same seed each time.
So calling Randomize only once for each file is exactly
what you don't want to do because you'll get the same
shuffle each time.

[toc] | [prev] | [next] | [standalone]


#11507

From"Auric__" <not.my.real@email.address>
Date2017-01-03 17:54 +0000
Message-ID<XnsA6F26F28A55F2auricauricauricauric@213.239.209.88>
In reply to#11496
Mayayana wrote:

> <doug.reiserrelief@gmail.com> wrote
>
>   I don't see the original post. I'm guessing it's
> coming out of Google Groups. You might consider
> using a real newsreader and a real usenet server,
> so that you can use the real usenet newsgroups.

"Doug" is a Google Groupie. The thread is from 2008, with several replies by 
old regs Pegasus, ekkehard.horner, Anthony Jones, axtens, and Dr J R 
Stockton. See here if you're interested (watch the wordwrap):

  https://groups.google.com/forum/#!
topic/microsoft.public.scripting.vbscript/b34hZ4TYjsk

>    In any case, Randomize should be called with
> every call to Shuffle. Randomize without a parameter
> calls Timer to get a seed number for shuffling. You can
> also use a unique numeric parameter if you like. Without
> calling Randomize you'll get the same seed each time.
> So calling Randomize only once for each file is exactly
> what you don't want to do because you'll get the same
> shuffle each time.

Randomize only needs to be called once per run. Calling it every time you 
enter a function is just a waste of cpu cycles.

-- 
The giver and the recipient may each define a "reward" quite differently.

[toc] | [prev] | [next] | [standalone]


#11509

From"Mayayana" <mayayana@invalid.nospam>
Date2017-01-03 19:38 -0500
Message-ID<o4hg39$68g$1@dont-email.me>
In reply to#11507
"Auric__" <not.my.real@email.address> wrote

| "Doug" is a Google Groupie. The thread is from 2008

Ah. I didn't notice the date.

| Randomize only needs to be called once per run. Calling it every time you
| enter a function is just a waste of cpu cycles.
|

   He said once per file. Once per run is what I'm
suggesting. In other words, if you randomize the
same string 3 times without calling Randomize then
the result will be 3 matching shuffles. So the best
approach is to call it with every Shuffle call, which
is hardly a waste of CPU cycles. It just retrieves
the system time. .... But maybe we're saying the same
thing. I'm not sure.

[toc] | [prev] | [next] | [standalone]


#11506

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-01-03 17:48 +0100
Message-ID<XnsA6F2B522EEB5eejj99@194.109.6.166>
In reply to#11494
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)

[toc] | [prev] | [next] | [standalone]


#11534

FromDr J R Stockton <reply1700@merlyn.demon.co.uk.invalid>
Date2017-01-05 19:23 +0000
Message-ID<g1wB4P1A1pbYFw0U@invalid.uk.co.demon.merlyn.invalid>
In reply to#11506
In microsoft.public.scripting.vbscript message <XnsA6F2B522EEB5eejj99@19
4.109.6.166>, Tue, 3 Jan 2017 17:48:23, Evertjan. <exxjxw.hannivoort@int
er.nl.net> posted:

>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" )

It might be interesting to compare the speeds of the array code and the
string code.


-- 
 (c) John Stockton, Surrey, UK.  ¬@merlyn.demon.co.uk   Turnpike v6.05   MIME.
 Merlyn Web Site <                       > - FAQish topics, acronyms, & links.

[toc] | [prev] | [next] | [standalone]


#11536

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-01-06 00:51 +0100
Message-ID<XnsA6F58AC8C59Aeejj99@194.109.6.166>
In reply to#11534
Dr J R Stockton <reply1700@merlyn.demon.co.uk.invalid> wrote on 05 Jan 2017 
in microsoft.public.scripting.vbscript:

> In microsoft.public.scripting.vbscript message <XnsA6F2B522EEB5eejj99@19
> 4.109.6.166>, Tue, 3 Jan 2017 17:48:23, Evertjan. <exxjxw.hannivoort@int
> er.nl.net> posted:
> 
>>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" )
> 
> It might be interesting to compare the speeds of the array code and the
> string code.

Be my guest. 

So far it is just array-theory and string-theory.

>>response.write ShuffleString( "ALLIGATOR" )

The clue might be here: <https://youtu.be/W0bidd0Uhvk>

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [standalone]


Back to top | Article view | microsoft.public.scripting.vbscript


csiph-web