![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #11 (permalink) |
| | Re: How to Randomize characters in a string Thanks. Worked great. "axtens" wrote: Quote: > 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 > > > > 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 Specs![]() |
| | #12 (permalink) |
| | RE: How to Randomize characters in a string "RICK" wrote: 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? 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" ) *********** Obviously, if you can create the original string in the array, you can bypass the copy from string to array. |
My System Specs![]() |
| | #13 (permalink) |
| | Re: How to Randomize characters in a string In microsoft.public.scripting.vbscript message <571A454D-2051-46AF- 9EF1-C7017CD27448@xxxxxx>, Tue, 19 Aug 2008 11:37:00, RICK <RICK@xxxxxx> posted: Quote: >Thanks. Worked great. > >"axtens" wrote: Quote: Quote: >> ' 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 9th number it must keep testing until it finds the right one. For nine or fewer numbers (starting with 1), however, such inefficiency will not actually make much difference. OTOH, for more numbers ISTM that the reject-unwanted will need to be recoded, since, by rejecting both genuine and spurious duplicates, it will not give a uniformly equi- probable result (and the inefficiency will increase). If it is necessary to generate a list of integers in a random order (not needed for this job) then a simple modification of the efficient shuffle will give an efficient deal, as shown on the cited site. -- (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3 news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>. <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links. |
My System Specs![]() |
| | #14 (permalink) |
| | Re: How to Randomize characters in a string In microsoft.public.scripting.vbscript message <E80766BD-B20E-4F81-9D51- 8CDB55269928@xxxxxx>, Tue, 19 Aug 2008 17:42:01, Old Pedant <OldPedant@xxxxxx> posted: Quote: >"RICK" wrote: > 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: >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! Quote: >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 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) |
My System Specs![]() |
| | #15 (permalink) |
| | Re: How to Randomize characters in a string "RICK" <RICK@xxxxxx> wrote in message news BB59609-64DA-4748-8B5E-4AD72E9BED58@xxxxxxQuote: > Actually what I want to do is to shuffle the characters. Any thoughts? > Since the characters are already random what purpose is served by shuffling them about? So far is seems sufficient just to move the last two characters about. -- Anthony Jones - MVP ASP/ASP.NET |
My System Specs![]() |
| | #16 (permalink) |
| | Re: How to Randomize characters in a string In microsoft.public.scripting.vbscript message <cd59c3e3-4e47-45a0-8fcb- 6dfdf55df355@xxxxxx>, Wed, 20 Aug 2008 22:00:24, axtens <Bruce.Axtens@xxxxxx> posted: Quote: >On Wed, 20 Aug 2008 17:15:54 +0100, Dr J R Stockton wrote: > Quote: >> That seems inefficient >Granted. However, in this situation, the "monte carlo" (if that's the >correct label) approach works, and the original poster is happy with >it. consider also that other people may see the code and use it in circumstances where such error could matter. For a discussion of the method, see <http://en.wikipedia.org/wiki/Knuth_ shuffle>. -- (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 Specs![]() |
| | #17 (permalink) |
| | Re: How to Randomize characters in a string On Aug 22, 5:22*pm, Dr J R Stockton <j...@xxxxxx> wrote: Quote: > * For K = 2 to L : J = L-K+1 > * * X = Random(J) : T = A(X) : A(X) = A(J) : A(J) = T '' SwapA(X) A(J) There's a bug somewhere in the test page ... not necessarily in that part ... -- (c) John Stockton, near London, UK. Posting with Google. Mail: J.R.""""""""@physics.org or (better) via Home Page at Web: <URL:http://www.merlyn.demon.co.uk/> FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ... |
My System Specs![]() |
| | #18 (permalink) |
| | Re: How to Randomize characters in a string On Aug 22, 7:10*pm, Dr J R Stockton <J.R.Stock...@xxxxxx> wrote: Quote: > On Aug 22, 5:22*pm, Dr J R Stockton <j...@xxxxxx> wrote: > Quote: > > * For K = 2 to L : J = L-K+1 > > * * X = Random(J) : T = A(X) : A(X) = A(J) : A(J) = T '' Swap A(X) A(J) > There's a bug somewhere in the test page ... not necessarily in that > part ... For K = 1 to L : J = L-K X = Random(J+1) : T = A(X) : A(X) = A(J) : A(J) = T '' Swap A(X) A(J) -- * (c) John Stockton, near London, UK. *Posting with Google. *Mail: J.R.""""""""@physics.org or (better) via Home Page at *Web: *<URL:http://www.merlyn.demon.co.uk/> *FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ... |
My System Specs![]() |
![]() |
| 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 | |||