Re: How to Randomize characters in a string

Tech-Archive recommends: Speed Up your PC by fixing your registry



In microsoft.public.scripting.vbscript message <E80766BD-B20E-4F81-9D51-
8CDB55269928@xxxxxxxxxxxxx>, Tue, 19 Aug 2008 17:42:01, Old Pedant
<OldPedant@xxxxxxxxxxxxxxxxxxxxxxxxx> posted:
"RICK" wrote:

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?

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!

only once per program execution.

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

In that code, for a string of length L, RND is called L times, and
Int(RND*L) has L equally-possible values 0..L-1. Therefore, there are
L*L equi-probable sequences when the code is generated, each generating
some string.

For a string containing L distinct characters, there are factorial(L)
possible orders. For any L > 2, l^2 is not a multiple of factorial(L).
Therefore, if I have read your code correctly, it cannot generate all
possible orders with equal probability. But (assuming RND is perfect)
if the multiplier of RND varies linearly between 1 & L, as in the code I
posted earlier, there are factorial(L) possibilities each generating a
different string; so all desired output strings are possible and equi-
probable.

Donald E Knuth :-
"Random numbers should not be generated with a method chosen at random".
Me : ^ or used

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
.



Relevant Pages

  • Re: In excel, I want to generate a random number
    ... elements back in the original array that was passed to it. ... the Randomize statement one time only. ... Dim RandomIndex As Long ... cards such as 3 of Hearts (here your DeckOfCards is declared as a String: ...
    (microsoft.public.excel.misc)
  • Re: In excel, I want to generate a random number
    ... Give it an array of elements and it will put them in random order and return the randomized elements back in the original array that was passed to it. ... Dim RandomIndex As Long ... The passed array may be of any normal type -- integer, string, single, etc. ... The neat thing is, if you pass an already randomized array to this routine, those randomly ordered elements will be randomize -- sort of like shuffling an already shuffled deck of cards. ...
    (microsoft.public.excel.misc)
  • RE: How to Randomize characters in a string
    ... would like to randomize those characters. ... if you don't *HAVE* to put the characters into a single ... Put them into an array. ... convert the string to an array. ...
    (microsoft.public.scripting.vbscript)
  • RE: How random are randomly generated numbers ?
    ... You really only need to issue the randomize statement once. ... Public Function APPCreateCode() As String ... Dim i As Integer ...
    (microsoft.public.access.formscoding)
  • Re: How to check to see if ALL boolean array elements are set to True
    ... Then I can randomize the numbers 1-100 and play ... CorrectChoice As String ... DText As String ... could be a long which stores the index of the correct answer in the array. ...
    (microsoft.public.vb.enterprise)