![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Randomize a one dimentional array Does anyone know how I can randomize a one dimentional array with VBscript. I would like it to pull back the values with no duplicates. e.g. mysting = "this" & vbcrlf & "is" & vbcrlf & "a" & vbcrlf & "test" myarr = split(mystring,vbcrlf) |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Randomize a one dimentional array On Thu, 02 Oct 2008 16:30:01 GMT, =?Utf-8?B?Qm9iIFNtaXRo?= wrote: Quote: > Does anyone know how I can randomize a one dimentional array with > VBscript. I would like it to pull back the values with no duplicates. > > e.g. > > mysting = "this" & vbcrlf & "is" & vbcrlf & "a" & vbcrlf & "test" > myarr = split(mystring,vbcrlf) each item is copied from the source array, replace it with the last item in the array, then decrease the size of the array by 1. When you hit 0, you're done. There are better ways to do this, of course, but this is a very simple method that I've used in a few places. This code should do what you want: RANDOMIZE TIMER source = ARRAY("this", "is", "a", "test") ubSrc = UBOUND(source) REDIM target(ubSrc) FOR L0 = ubSrc TO 1 STEP -1 ' the actual work is in this loop tmp = INT(RND * (L1 + 1)) target(L1) = source(tmp) source(tmp) = source(L1) REDIM PRESERVE source(L1 - 1) NEXT target(0) = source(0) outP = target(0) FOR L0 = 1 TO ubSrc outP = outP & VBCRLF & target(L1) NEXT MSGBOX outP This works for many similar situations, i.e. card shuffling. -- What is the difference between Nintendo64 and Commodore64? |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Randomize a one dimentional array Bob Smith schrieb: Quote: > Does anyone know how I can randomize a one dimentional array with VBscript. I > would like it to pull back the values with no duplicates. > > e.g. > > mysting = "this" & vbcrlf & "is" & vbcrlf & "a" & vbcrlf & "test" > myarr = split(mystring,vbcrlf) > in this group |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Randomize a one dimentional array Auric__ schrieb: Quote: > On Thu, 02 Oct 2008 16:30:01 GMT, =?Utf-8?B?Qm9iIFNtaXRo?= wrote: > Quote: >> Does anyone know how I can randomize a one dimentional array with >> VBscript. I would like it to pull back the values with no duplicates. >> >> e.g. >> >> mysting = "this" & vbcrlf & "is" & vbcrlf & "a" & vbcrlf & "test" >> myarr = split(mystring,vbcrlf) > Copy the items in random order from the source array to a second array. As > each item is copied from the source array, replace it with the last item in > the array, then decrease the size of the array by 1. When you hit 0, you're > done. > > There are better ways to do this, of course, but this is a very simple > method that I've used in a few places. This code should do what you want: > > RANDOMIZE TIMER > source = ARRAY("this", "is", "a", "test") > ubSrc = UBOUND(source) > > REDIM target(ubSrc) > > FOR L0 = ubSrc TO 1 STEP -1 ' the actual work is in this loop > tmp = INT(RND * (L1 + 1)) > target(L1) = source(tmp) > source(tmp) = source(L1) > REDIM PRESERVE source(L1 - 1) here immediately: (L1 - 1) == -1. Quote: > NEXT > > target(0) = source(0) > > outP = target(0) > FOR L0 = 1 TO ubSrc > outP = outP & VBCRLF & target(L1) > NEXT > > MSGBOX outP > > This works for many similar situations, i.e. card shuffling. > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Randomize a one dimentional array Thanks Auric__ but the code does not seem to work. subscript out of range: 'tmp'. I'm not sure why but it looks like value L1 is not defined? What should L1 be? "Auric__" wrote: Quote: > On Thu, 02 Oct 2008 16:30:01 GMT, =?Utf-8?B?Qm9iIFNtaXRo?= wrote: > Quote: > > Does anyone know how I can randomize a one dimentional array with > > VBscript. I would like it to pull back the values with no duplicates. > > > > e.g. > > > > mysting = "this" & vbcrlf & "is" & vbcrlf & "a" & vbcrlf & "test" > > myarr = split(mystring,vbcrlf) > Copy the items in random order from the source array to a second array. As > each item is copied from the source array, replace it with the last item in > the array, then decrease the size of the array by 1. When you hit 0, you're > done. > > There are better ways to do this, of course, but this is a very simple > method that I've used in a few places. This code should do what you want: > > RANDOMIZE TIMER > source = ARRAY("this", "is", "a", "test") > ubSrc = UBOUND(source) > > REDIM target(ubSrc) > > FOR L0 = ubSrc TO 1 STEP -1 ' the actual work is in this loop > tmp = INT(RND * (L1 + 1)) > target(L1) = source(tmp) > source(tmp) = source(L1) > REDIM PRESERVE source(L1 - 1) > NEXT > > target(0) = source(0) > > outP = target(0) > FOR L0 = 1 TO ubSrc > outP = outP & VBCRLF & target(L1) > NEXT > > MSGBOX outP > > This works for many similar situations, i.e. card shuffling. > > -- > What is the difference between Nintendo64 and Commodore64? > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Randomize a one dimentional array On Thu, 02 Oct 2008 17:57:20 GMT, ekkehard.horner wrote: Quote: > Auric__ schrieb: Quote: >> On Thu, 02 Oct 2008 16:30:01 GMT, =?Utf-8?B?Qm9iIFNtaXRo?= wrote: >> Quote: >>> Does anyone know how I can randomize a one dimentional array with >>> VBscript. I would like it to pull back the values with no duplicates. >>> >>> e.g. >>> >>> mysting = "this" & vbcrlf & "is" & vbcrlf & "a" & vbcrlf & "test" >>> myarr = split(mystring,vbcrlf) >> Copy the items in random order from the source array to a second array. >> As each item is copied from the source array, replace it with the last >> item in the array, then decrease the size of the array by 1. When you >> hit 0, you're done. >> >> There are better ways to do this, of course, but this is a very simple >> method that I've used in a few places. This code should do what you >> want: >> >> RANDOMIZE TIMER >> source = ARRAY("this", "is", "a", "test") >> ubSrc = UBOUND(source) >> >> REDIM target(ubSrc) >> >> FOR L0 = ubSrc TO 1 STEP -1 ' the actual work is in this loop >> tmp = INT(RND * (L1 + 1)) >> target(L1) = source(tmp) >> source(tmp) = source(L1) >> REDIM PRESERVE source(L1 - 1) > Because L1 is not initialized/empty/0, source is clobbered > here immediately: (L1 - 1) == -1. doesn't matter). Should be: FOR L0 = ubSrc TO 1 STEP -1 tmp = INT(RND * (L0 + 1)) target(L0) = source(tmp) source(tmp) = source(L0) REDIM PRESERVE source(L0 - 1) NEXT -- There might be room at the top but there is seldom enough time. |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Randomize a one dimentional array On Thu, 02 Oct 2008 18:14:12 GMT, =?Utf-8?B?Qm9iIFNtaXRo?= wrote: Quote: > Thanks Auric__ but the code does not seem to work. subscript out of > range: 'tmp'. I'm not sure why but it looks like value L1 is not > defined? What should L1 be? Corrected version, works for me: RANDOMIZE TIMER source = ARRAY("this", "is", "a", "test") ubSrc = UBOUND(source) REDIM target(ubSrc) FOR L1 = ubSrc TO 1 STEP -1 tmp = INT(RND * (L1 + 1)) target(L1) = source(tmp) source(tmp) = source(L1) REDIM PRESERVE source(L1 - 1) NEXT target(0) = source(0) outP = target(0) FOR L1 = 1 TO ubSrc outP = outP & VBCRLF & target(L1) NEXT MSGBOX outP (That's what I get for posting before my daily caffiene IV. [sigh]) -- Welcome and keep out. |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Randomize a one dimentional array Auric__ schrieb: Quote: > On Thu, 02 Oct 2008 17:57:20 GMT, ekkehard.horner wrote: > Quote: >> Auric__ schrieb: Please test your code before posting - now you generate an array with 4 identical string items. |
My System Specs![]() |
| | #9 (permalink) |
| | Re: Randomize a one dimentional array ekkehard.horner schrieb: Quote: > Auric__ schrieb: Quote: >> On Thu, 02 Oct 2008 17:57:20 GMT, ekkehard.horner wrote: >> Quote: >>> Auric__ schrieb: > Please test your code before posting - now you generate an > array with 4 identical string items. code repeats the L1==0==uninitialized value |
My System Specs![]() |
| | #10 (permalink) |
| | Re: Randomize a one dimentional array On Thu, 02 Oct 2008 19:00:53 GMT, ekkehard.horner wrote: Quote: > ekkehard.horner schrieb: Quote: >> Auric__ schrieb: Quote: >>> On Thu, 02 Oct 2008 17:57:20 GMT, ekkehard.horner wrote: >>> >>>> Auric__ schrieb: >> Please test your code before posting - now you generate an >> array with 4 identical string items. > code repeats the L1==0==uninitialized value I should've just posted my actual code (a card shuffling test) that does this: -----begin shufler4.vbs----- Option Explicit Const ForReading = 1 Const ForWriting = 2 Dim unShuffled Dim Shuffled(51) Dim L0, card, fO Dim FSO unShuffled = Array("AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", _ "10S", "JS", "QS", "KS", "AH", "2H", "3H", "4H", "5H", "6H", "7H", _ "8H", "9H", "10H", "JH", "QH", "KH", "AC", "2C", "3C", "4C", "5C", _ "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", "AD", "2D", "3D", _ "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD") Randomize Timer For L0 = 51 To 0 Step -1 card = Int(Rnd * (L0 + 1)) Shuffled(L0) = unShuffled(card) If card <> L0 Then unShuffled(card) = unShuffled(L0) Next Set FSO = WScript.CreateObject("Scripting.FileSystemObject") Set fO = FSO.OpenTextFile("shuf4.txt", ForWriting, True) For L0 = 0 To 51 fO.WriteLine Shuffled(L0) Next fO.Close -----end shufler4.vbs----- (Note that I don't resize the array in this version.) -- kill -9 needs no justification! -- BOfH |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Fast copy method of sub array (=array range) possible? | VB Script | |||
| Search a Two-Dimentional Array | VB Script | |||
| Stupid Array Tricks: Initializing an Array to a Certain Size | PowerShell | |||
| How to Randomize characters in a string | VB Script | |||
| how to assign values to array and how to create array via variable | PowerShell | |||