Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - How to Randomize characters in a string

Reply
 
Old 08-19-2008   #1 (permalink)
RICK


 
 

How to Randomize characters in a string

I have a string of of characters that are being stored in a variable. I
would like to randomize those characters. How can I do that?

Rick

My System SpecsSystem Spec
Old 08-19-2008   #2 (permalink)
Pegasus \(MVP\)


 
 

Re: How to Randomize characters in a string


"RICK" <RICK@xxxxxx> wrote in message
news:F73BB2A2-98D0-4BBC-B3E5-2C347A597962@xxxxxx
Quote:

>I have a string of of characters that are being stored in a variable. I
> would like to randomize those characters. How can I do that?
>
> Rick
If your string contains n characters then you could use the rnd
function to generate n integers between 1 and n, making sure
that each number is unique. You then use these numbers to
rearrange the characters in your string.


My System SpecsSystem Spec
Old 08-19-2008   #3 (permalink)
RICK


 
 

Re: How to Randomize characters in a string

But, my characters are alphanumeric.

What I have done is to create seven random letters followed by one random
number and one random special character.

I now want to randomize these nine characters into another

"Pegasus (MVP)" wrote:
Quote:

>
> "RICK" <RICK@xxxxxx> wrote in message
> news:F73BB2A2-98D0-4BBC-B3E5-2C347A597962@xxxxxx
Quote:

> >I have a string of of characters that are being stored in a variable. I
> > would like to randomize those characters. How can I do that?
> >
> > Rick
>
> If your string contains n characters then you could use the rnd
> function to generate n integers between 1 and n, making sure
> that each number is unique. You then use these numbers to
> rearrange the characters in your string.
>
>
>
My System SpecsSystem Spec
Old 08-19-2008   #4 (permalink)
Pegasus \(MVP\)


 
 

Re: How to Randomize characters in a string

Let's assume your have name="Rick". Using my method, you generate
an array containing four randomised numbers, e.g.
arr(1) = 4
arr(2) = 1
arr(3) = 3
arr(4) = 2
Your randomised string will now be
mid(name, arr(1), 1) & mid(name(arr(2), 1) & mid(name, arr(3), 1) &
mid(name(arr(4), 1)

You must, of course, automate the process instead of writing it
out as above.


"RICK" <RICK@xxxxxx> wrote in message
news:A9EE9112-5780-4DD3-A2C3-BADBEC32CCCB@xxxxxx
Quote:

> But, my characters are alphanumeric.
>
> What I have done is to create seven random letters followed by one random
> number and one random special character.
>
> I now want to randomize these nine characters into another
>
> "Pegasus (MVP)" wrote:
>
Quote:

>>
>> "RICK" <RICK@xxxxxx> wrote in message
>> news:F73BB2A2-98D0-4BBC-B3E5-2C347A597962@xxxxxx
Quote:

>> >I have a string of of characters that are being stored in a variable. I
>> > would like to randomize those characters. How can I do that?
>> >
>> > Rick
>>
>> If your string contains n characters then you could use the rnd
>> function to generate n integers between 1 and n, making sure
>> that each number is unique. You then use these numbers to
>> rearrange the characters in your string.
>>
>>
>>

My System SpecsSystem Spec
Old 08-19-2008   #5 (permalink)
ekkehard.horner


 
 

Re: How to Randomize characters in a string

RICK schrieb:
Quote:

> I have a string of of characters that are being stored in a variable. I
> would like to randomize those characters. How can I do that?
>
> Rick
Split the string into an array of characters and shuffle the array:

Option Explicit

WScript.Quit testShuffleString()

''= tests string shuffle (shuffleArray(), str2CharArr())
' ============================================================================
Function testShuffleString()
Dim aSeeds : aSeeds = Array( _
"ABCDE", "", "AABBCC", "rambomize", "11111511111" _
)
Dim sSeed
For Each sSeed In aSeeds
WScript.Echo "--- |" & sSeed & "|"
Dim nIdx
For nIdx = 0 To 1 + Len( sSeed )
' WScript.Echo " |" & shuffleS( sSeed ) & "|"
WScript.Echo " |" & Join( shuffleArray( str2CharArr( sSeed ) ), "" ) & "|"
Next
WScript.Echo
Next

testShuffleString = 0
End Function

''= shuffles one dimensional array
' ============================================================================
Function shuffleArray( ByVal a1D )
Dim nTo : nTo = UBound( a1D ) + 1
Dim nIdx, nPos, vTmp
For nIdx = 0 To UBound( a1D )
nPos = IRandR( nIdx, nTo )
vTmp = a1D( nIdx )
a1D( nIdx ) = a1D( nPos )
a1D( nPos ) = vTmp
Next
shuffleArray = a1D
End Function

''= splits string into array of chars
' ============================================================================
Function str2CharArr( sTxt )
ReDim aChars( Len( sTxt ) - 1 )
Dim nPos
For nPos = 0 To UBound( aChars )
aChars( nPos ) = Mid( sTxt, nPos + 1, 1 )
Next
str2CharArr = aChars
End Function

''= generates 'random' integer between nFrom and (nTo - 1)
' ============================================================================
Function IRandR( ByVal nFrom, ByVal nTo )
IRandR = nFrom + Int( Rnd * (nTo - nFrom) )
End Function
My System SpecsSystem Spec
Old 08-19-2008   #6 (permalink)
Anthony Jones


 
 

Re: How to Randomize characters in a string

"RICK" <RICK@xxxxxx> wrote in message
news:A9EE9112-5780-4DD3-A2C3-BADBEC32CCCB@xxxxxx
Quote:

> But, my characters are alphanumeric.
>
> What I have done is to create seven random letters followed by one random
> number and one random special character.
>
> I now want to randomize these nine characters into another

What is meant by randomize in that last sentence? Do you actually mean
arrange the characters into another random order?

Since the letters are choosen at random any way all you actually need is it
choose two random numbers that indicate the ordinal position of the number
and the special character.

What structure are the characters currently held in, a string or as elements
of an array?

--
Anthony Jones - MVP ASP/ASP.NET


My System SpecsSystem Spec
Old 08-19-2008   #7 (permalink)
axtens


 
 

Re: How to Randomize characters in a string

On Aug 19, 10:52*pm, RICK <R...@xxxxxx> wrote:
Quote:

> But, my characters are alphanumeric.
>
> What I have done is to create seven random letters followed by one random
> number and one random special character.
>
> I now want to randomize these nine characters into another
>
Rick,

I hope the code is fairly self-explanatory. Basically, it finds random
numbers between 1 and 9, removing them from pattern (the acceptable
choices) until pattern is empty, and appends those found to selection.
Then it iterates through selection, using the numbers therein as
offsets into original.

Kind regards,
Bruce.



dim a
dim n
dim original
dim pattern
dim selection
dim final
dim i

randomize timer

original = "abcdefg1@"
pattern = "123456789"
selection = ""
final = ""

' pick 9 random numbers without duplicates
do
n = int( rnd * 9 ) + 1

i = instr(pattern, cstr(n))
if i > 0 then
pattern = left(pattern,i-1) & mid(pattern,i+1)
selection = selection & cstr(n)
end if
if len(pattern) = 0 then exit do
loop

'use those numbers to select items from the original
for i = 1 to len(selection)
n = mid(selection,i,1)
final = final & mid(original,n,1)
next

wscript.echo original, selection, final

---
Sample run:

abcdefg1@ 329675841 cb@xxxxxx
My System SpecsSystem Spec
Old 08-19-2008   #8 (permalink)
RICK


 
 

Re: How to Randomize characters in a string

Actually what I want to do is to shuffle the characters. Any thoughts?

"Anthony Jones" wrote:
Quote:

> "RICK" <RICK@xxxxxx> wrote in message
> news:A9EE9112-5780-4DD3-A2C3-BADBEC32CCCB@xxxxxx
Quote:

> > But, my characters are alphanumeric.
> >
> > What I have done is to create seven random letters followed by one random
> > number and one random special character.
> >
> > I now want to randomize these nine characters into another
>
>
> What is meant by randomize in that last sentence? Do you actually mean
> arrange the characters into another random order?
>
> Since the letters are choosen at random any way all you actually need is it
> choose two random numbers that indicate the ordinal position of the number
> and the special character.
>
> What structure are the characters currently held in, a string or as elements
> of an array?
>
> --
> Anthony Jones - MVP ASP/ASP.NET
>
>
>
My System SpecsSystem Spec
Old 08-19-2008   #9 (permalink)
RICK


 
 

Re: How to Randomize characters in a string

And to answer your second question the characters are stored in a named string.

"Anthony Jones" wrote:
Quote:

> "RICK" <RICK@xxxxxx> wrote in message
> news:A9EE9112-5780-4DD3-A2C3-BADBEC32CCCB@xxxxxx
Quote:

> > But, my characters are alphanumeric.
> >
> > What I have done is to create seven random letters followed by one random
> > number and one random special character.
> >
> > I now want to randomize these nine characters into another
>
>
> What is meant by randomize in that last sentence? Do you actually mean
> arrange the characters into another random order?
>
> Since the letters are choosen at random any way all you actually need is it
> choose two random numbers that indicate the ordinal position of the number
> and the special character.
>
> What structure are the characters currently held in, a string or as elements
> of an array?
>
> --
> Anthony Jones - MVP ASP/ASP.NET
>
>
>
My System SpecsSystem Spec
Old 08-19-2008   #10 (permalink)
Dr J R Stockton


 
 

Re: How to Randomize characters in a string

In microsoft.public.scripting.vbscript message <#OaAWkgAJHA.2060@xxxxxx
NGP05.phx.gbl>, Tue, 19 Aug 2008 16:37:17, "Pegasus (MVP)"
<I.can@xxxxxx> posted:
Quote:

>
>"RICK" <RICK@xxxxxx> wrote in message
>news:F73BB2A2-98D0-4BBC-B3E5-2C347A597962@xxxxxx
Quote:

>>I have a string of of characters that are being stored in a variable. I
>> would like to randomize those characters. How can I do that?
Quote:

>If your string contains n characters then you could use the rnd
>function to generate n integers between 1 and n, making sure
>that each number is unique. You then use these numbers to
>rearrange the characters in your string.
I trust that you do not select algorithms professionally. Shuffling is
a standard task, for which you need only read Knuth. The efficient
method is considerably simpler.



The OP may be helped by <URL:http://www.merlyn.demon.co.uk/vb-maths.htm>
which for details cites <URL:http://www.merlyn.demon.co.uk/js-randm.htm>
and <URL:http://www.merlyn.demon.co.uk/pas-rand.htm> (I don't recall
needing Shuffle in VB before now).

function Random(N) '' Return a random integer in 0..(N-1)
Random = Int(N*Rnd)
end function

Then translate from Pascal, assuming the string has been split into an
array 1..N of characters,
for J := Max downto 2 do Swap(A[J], A[1+Random(J)]) ;
(* note : those are calls by reference,

Note that the above Pascal line executes in N(N-1)(N-2)..2 equi-probable
ways (depending on the Random results), thus providing exactly enough
randomness for the N! orders of characters in a string (assumed all
different). Then rejoin the string.

I suspect Ekkehard's code uses basically the same Shuffle algorithm.

One can of course use Mid and & to create a Swap acting directly on a
string. My feeling is that it would be slower. But, if speed matters,
test it.

--
(c) John Stockton, near London. *@merlyn.demon.co.uk/?.?.Stockton@xxxxxx
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Remove characters in a string VB Script
Select first 16 characters in a string PowerShell
Replacing Multiple Characters In A String PowerShell
Capture a string of characters and use as filename. PowerShell
Removing characters from a string PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46